Home > other >  App works in emulator but crashes on a real device
App works in emulator but crashes on a real device

Time:04-05

In emulator, my app works perfectly fine but when I use the app on my real Samsung Galaxy device, the app will crash. I have now debugged it and see this error:

2022-04-02 22:41:18.584 16611-16611/? E/USNET: USNET: appName: ch.yourclick.ki 2022-04-02 22:41:19.546 16611-16611/ch.yourclick.ki E/AndroidRuntime: FATAL EXCEPTION: main Process: ch.yourclick.ki, PID: 16611 java.lang.RuntimeException: Unable to start service ch.yourclick.ki.services.PicovoiceService@d2ab860 with Intent { cmp=ch.yourclick.ki/.services.PicovoiceService (has extras) }: java.lang.IllegalArgumentException: ch.yourclick.ki: Targeting S (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles. at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:5110) at android.app.ActivityThread.access$2200(ActivityThread.java:310) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2319) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:226) at android.os.Looper.loop(Looper.java:313) at android.app.ActivityThread.main(ActivityThread.java:8663) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135) Caused by: java.lang.IllegalArgumentException: ch.yourclick.ki: Targeting S (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles. at android.app.PendingIntent.checkFlags(PendingIntent.java:382) at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:465) at android.app.PendingIntent.getActivity(PendingIntent.java:451) at android.app.PendingIntent.getActivity(PendingIntent.java:415) at ch.yourclick.ki.services.PicovoiceService.getNotification(PicovoiceService.java:142) at ch.yourclick.ki.services.PicovoiceService.onStartCommand(PicovoiceService.java:127) at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:5092) ... 9 more

This is how I start the service:

mDialog.startService(getContext()); // <-- App only crashes when I use this line
public void startService(Context context) {
    mServiceIntent = new Intent(context, PicovoiceService.class);
    mServiceIntent.putExtra("keywordFileName", "Hey-Ki_de_android_v2_1_0.ppn");
    mServiceIntent.putExtra("contextFileName", "Ki_de_android_v2_1_0.rhn");
    ContextCompat.startForegroundService(context, mServiceIntent);
}

In my app everything works fine, so the app only crashes when I press on a button to start the service PicovoiceService.class.

What is wrong with my code and why does it work in the emulator but not on a real device?

CodePudding user response:

According to the stack, this seems like the relevant cause of your crash: java.lang.IllegalArgumentException: ch.yourclick.ki: Targeting S (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable

Regarding your question as to why it works in the emulator and not on your physical device, your emulator is running API 30, which is below the requirement. If you ran an emulator with API 31, you would experience this crash just as you do on the physical device.

  • Related