How to make the transition from Intent to PendingIntent in startActivityAndCollapse() in TileService?

125 views Asked by At

Since Android API 34, startActivityAndCollapse(Intent intent) is deprecated, and the official document recommends using PendingIntent instead.

Not familiar with PendingIntent, I tried the transition from Intent to PendingIntent, but some runtime errors came out, and ANR in the app was noticed.

The followings are:

  1. the original code using Intent.
  2. the current code using PendingIntent (with runtime errors).
  3. the runtime errors shown in the Logcat of Android Studio.

How to make the transition right?

  1. the original code using Intent:
private fun startActivityBasedOnScreenStatus(intent: Intent) {
        if (keyguardManager.isKeyguardLocked) {
            startActivity(intent)
        } else {
            startActivityAndCollapse(intent)
        }
    }
  1. the current code using PendingIntent (with runtime errors):
@SuppressLint("StartActivityAndCollapseDeprecated")
    private fun startActivityBasedOnScreenStatus(intent: Intent) {
        if (keyguardManager.isKeyguardLocked) {
            startActivity(intent)
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
            startActivityAndCollapse(
                PendingIntent.getActivity(
                    applicationContext,
                    0,
                    intent,
                    PendingIntent.FLAG_UPDATE_CURRENT
                )
            )
        } else {
            @Suppress("DEPRECATION")
            startActivityAndCollapse(intent)
        }
    }
  1. the runtime errors shown in the Logcat of Android Studio:
---------------------------- PROCESS STARTED (1556) for package com.kkhtw.flashlight ----------------------------
2023-12-07 11:46:28.464  1992-2161  WindowManager           system_server                        V      startWCT=WindowContainerTransaction { changes = {} hops = [] errorCallbackToken=null taskFragmentOrganizer=null }
2023-12-07 11:46:28.694  1312-1312  CamX                    [email protected]  E  [ERROR][CHI    ] camxchicontext.cpp:5784 GetSessionData() Did not find any value for the given context key.
2023-12-07 11:46:28.705  1312-1312  CamX                    [email protected]  E  [ERROR][CHI    ] camxchicontext.cpp:5784 GetSessionData() Did not find any value for the given context key.
2023-12-07 11:46:28.889  1556-2968  chromium                com.kkhtw.flashlight                 E  [1207/114628.889465:ERROR:variations_seed_loader.cc(69)] Failed to open file for reading. Errno: 2
2023-12-07 11:46:29.657  1556-3139  Ads                     com.kkhtw.flashlight                 W  Invoke Firebase method getInstance error.
2023-12-07 11:46:37.394  1992-3357  ActivityManager         system_server                        E  ANR in com.kkhtw.flashlight (com.kkhtw.flashlight/.MainActivity)
                                                                                                    PID: 1556
                                                                                                    Reason: Input dispatching timed out (5dba712 com.kkhtw.flashlight/com.kkhtw.flashlight.MainActivity (server) is not responding. Waited 5002ms for FocusEvent(hasFocus=true))
                                                                                                    Parent: com.kkhtw.flashlight/.MainActivity
                                                                                                    ErrorId: 7c1b5882-3923-4e08-9c0a-ad40f9fe3697
                                                                                                    Frozen: false
                                                                                                    Load: 1.84 / 2.61 / 2.33
                                                                                                    ----- Output from /proc/pressure/memory -----
                                                                                                    some avg10=0.12 avg60=0.03 avg300=0.02 total=38361876
                                                                                                    full avg10=0.00 avg60=0.00 avg300=0.00 total=10853637
                                                                                                    ----- End output from /proc/pressure/memory -----
                                                                                                    ----- Output from /proc/pressure/cpu -----
                                                                                                    some avg10=2.17 avg60=1.84 avg300=2.33 total=723752390
                                                                                                    ----- End output from /proc/pressure/cpu -----
                                                                                                    ----- Output from /proc/pressure/io -----
                                                                                                    some avg10=0.01 avg60=0.16 avg300=0.07 total=92657013
                                                                                                    full avg10=0.00 avg60=0.07 avg300=0.02 total=51966787
                                                                                                    ----- End output from /proc/pressure/io -----
                                                                                                    
                                                                                                    CPU usage from 46486ms to -1ms ago (2023-12-07 11:45:48.343 to 2023-12-07 11:46:34.830):
                                                                                                      73% 2023/com.cathay.mymobione:xdvdydzdudodkdqdeiyizioikijiqixezeuekeqedwiwewxwhwpwvwywzwlwowtwgwjwqwxsys:wd: 73% user + 0.6% kernel / faults: 35924 minor
                                                                                                      5% 1556/com.kkhtw.flashlight: 4.7% user + 0.2% kernel / faults: 43419 minor 1 major
                                                                                                      11% 1992/system_server: 7.6% user + 3.7% kernel / faults: 16091 minor 8 major
                                                                                                      4.9% 2881/com.android.systemui: 3.7% user + 1.1% kernel / faults: 6532 minor 9 major
                                                                                                      4.7% 659/surfaceflinger: 3% user + 1.6% kernel / faults: 130 minor 2 major
                                                                                                      2.9% 3378/com.google.android.gms.persistent: 2.2% user + 0.6% kernel / faults: 6523 minor 20 major
                                                                                                      2.8% 519/crtc_event:105: 0% user + 2.8% kernel
                                                                                                      2.7% 661/vendor.qti.hardware.display.composer-service: 1.8% user + 0.9% kernel / faults: 1209 minor
                                                                                                      1.5% 29452/com.google.android.apps.nexuslauncher: 1.2% user + 0.3% kernel / faults: 1528 minor
                                                                                                      0.5% 1309/android.hardware.audio.service: 0.2% user + 0.2% kernel / faults: 14 minor 24 major
                                                                                                    19% TOTAL: 15% user + 3.3% kernel + 0% iowait + 0.6% irq + 0.1% softirq
                                                                                                    CPU usage from 67ms to 650ms later (2023-12-07 11:46:34.896 to 2023-12-07 11:46:35.479):
                                                                                                      197% 1556/com.kkhtw.flashlight: 193% user + 4.4% kernel / faults: 1046 minor
                                                                                                        101% 1556/khtw.flashlight: 96% user + 4.4% kernel
                                                                                                        94% 1569/ReferenceQueueD: 94% user + 0% kernel
                                                                                                        6.7% 1567/HeapTaskDaemon: 4.4% user + 2.2% kernel
                                                                                                      140% 1992/system_server: 71% user + 69% kernel / faults: 6223 minor
                                                                                                        78% 2045/Signal Catcher: 55% user + 23% kernel
                                                                                                        52% 3358/AnrAuxiliaryTas: 13% user + 39% kernel
                                                                                                        4.6% 2671/SensorService: 2.3% user + 2.3% kernel
                                                                                                        2.3% 2707/ackgroundThread: 0% user + 2.3% kernel
                                                                                                        2.3% 4931/binder:1992_13: 2.3% user + 0% kernel
                                                                                                      100% 2023/com.cathay.mymobione:xdvdydzdudodkdqdeiyizioikijiqixezeuekeqedwiwewxwhwpwvwywzwlwowtwgwjwqwxsys:wd: 100% user + 0% kernel / faults: 35 minor
                                                                                                        100% 2110/Thread-10: 100% user + 0% kernel
                                                                                                       +0% 3368/Timer-35: 0% user + 0% kernel
                                                                                                      19% 4738/com.google.android.apps.inputmethod.zhuyin: 17% user + 2.4% kernel / faults: 1235 minor
                                                                                                        19% 4749/Signal Catcher: 17% user + 2.4% kernel
                                                                                                      1.7% 72/rcuop/7: 0% user + 1.7% kernel
                                                                                                      2.2% 1331/[email protected]: 0% user + 2.2% kernel
                                                                                                        2.2% 2643/[email protected]: 0% user + 2.2% kernel
                                                                                                      2.4% 3993/irq/44-90cd000.: 0% user + 2.4% kernel
                                                                                                      3.1% 13043/adbd: 3.1% user + 0% kernel
                                                                                                        3.1% 13043/adbd: 3.1% user + 0% kernel
                                                                                                      3.5% 27139/kworker/u16:10-kverityd: 0% user + 3.5% kernel
                                                                                                    57% TOTAL: 45% user + 10% kernel + 0.8% irq
2023-12-07 11:46:38.715  9866-1196  SQLiteDatabase          com.google.android.apps.turbo        E  Error inserting timezone=Asia/Taipei timestamp_millis=1701920748000 battery_level=95 battery_saver=2 charge_type=0 place_key=null
                                                                                                    android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: battery_event.timestamp_millis (code 1555 SQLITE_CONSTRAINT_PRIMARYKEY)
                                                                                                        at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
                                                                                                        at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:961)
                                                                                                        at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:790)
                                                                                                        at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:89)
                                                                                                        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1870)
                                                                                                        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1739)
                                                                                                        at com.google.android.apps.turbo.nudges.broadcasts.BatteryHistoryLoggerJobService.f(PG:164)
                                                                                                        at avc.accept(PG:1)
                                                                                                        at java.util.ArrayList.forEach(ArrayList.java:1528)
                                                                                                        at com.google.android.apps.turbo.nudges.broadcasts.BatteryHistoryLoggerJobService.d(PG:37)
                                                                                                        at aty.run(PG:65)
                                                                                                        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:487)
                                                                                                        at dor.a(PG:1)
                                                                                                        at doa.run(PG:4)
                                                                                                        at dos.run(PG:1)
                                                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
                                                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
                                                                                                        at java.lang.Thread.run(Thread.java:1012)
2023-12-07 11:46:38.717  9866-1196  SQLiteDatabase          com.google.android.apps.turbo        E  Error inserting timezone=Asia/Taipei timestamp_millis=1701920748000 battery_level=95 battery_saver=2 charge_type=2 place_key=null
                                                                                                    android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: battery_event.timestamp_millis (code 1555 SQLITE_CONSTRAINT_PRIMARYKEY)
                                                                                                        at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
                                                                                                        at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:961)
                                                                                                        at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:790)
                                                                                                        at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:89)
                                                                                                        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1870)
                                                                                                        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1739)
                                                                                                        at com.google.android.apps.turbo.nudges.broadcasts.BatteryHistoryLoggerJobService.f(PG:164)
                                                                                                        at avc.accept(PG:1)
                                                                                                        at java.util.ArrayList.forEach(ArrayList.java:1528)
                                                                                                        at com.google.android.apps.turbo.nudges.broadcasts.BatteryHistoryLoggerJobService.d(PG:37)
                                                                                                        at aty.run(PG:65)
                                                                                                        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:487)
                                                                                                        at dor.a(PG:1)
                                                                                                        at doa.run(PG:4)
                                                                                                        at dos.run(PG:1)
                                                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
                                                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
                                                                                                        at java.lang.Thread.run(Thread.java:1012)
2023-12-07 11:46:49.164  1992-3419  ActivityManager         system_server                        I  Skipping duplicate ANR: com.android.server.am.ProcessErrorStateRecord@edbe253 executing service com.kkhtw.flashlight/.MyTileService
2023-12-07 11:47:10.843  1312-1312  CamX                    [email protected]  E  [ERROR][CHI    ] camxchicontext.cpp:5784 GetSessionData() Did not find any value for the given context key.
2023-12-07 11:47:10.849  1312-1312  CamX                    [email protected]  E  [ERROR][CHI    ] camxchicontext.cpp:5784 GetSessionData() Did not find any value for the given context key.
2023-12-07 11:47:11.003  1312-1312  CamX                    [email protected]  E  [ERROR][CHI    ] camxchicontext.cpp:5784 GetSessionData() Did not find any value for the given context key.
2023-12-07 11:47:11.008  1312-1312  CamX                    [email protected]  E  [ERROR][CHI    ] camxchicontext.cpp:5784 GetSessionData() Did not find any value for the given context key.
0

There are 0 answers