-1

I'm trying to learn how to use background services and have been watching videos, recreating working demos, etc to understand how it all works. I've now run two different demos that seem to work on Android 8 but fail on Android 12 (I try to run the app and i:t crashes immediately. I restart it again and it crashes and gives me the error ("Example" being the app name in this case):

"Clear Cache for Example? Example is closed because the app has a bug. Try clearing the app's cache first and then reopen the app"

Unfortunately, I don't really know what's breaking, so I'm not sure how to fix it . . . any idea what might be causing my problems? Here is one example that's clearly documented for reference:

https://erev0s.com/blog/run-android-service-background-reliably-every-n-seconds/

Any ideas what might be causing it? Any help you can give me would be greatly appreciated. Thanks!

Bob Dole
  • 31
  • 3
  • 1
    Many things, you do need to look at the error that is shown in e.g. Android Studio at the bottom https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – zapl Sep 29 '22 at 10:22
  • 1
    Check error in Android Studio like @zapl said. Probably this is cause Android 12 (API 31) changes for background/foreground services. You have to explicitly set a value to this attribute e.g "android:exported="false/true" – damienG Sep 29 '22 at 11:08
  • @zapl Thanks for this recommendation, I was in the build window and didn't get any feedback from the app there (which makes sense). Opening the logcat file made it blatently obvious what the issue was and it was a simple fix. Thanks for taking the time to point me that direction! Still very new but this helps a lot! – Bob Dole Oct 05 '22 at 18:28

1 Answers1

1

If you are using any pending intents use the following as flags

PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_IMMUTABLE

in Android 12 or above. It might solve your issue

Supun Ayeshmantha
  • 499
  • 1
  • 5
  • 9
  • 1
    You hit the nail right on the head, thanks! Going through the logcat files shows this is the core issue. Thanks again! – Bob Dole Oct 05 '22 at 18:27