0

I have an app that runs a stopwatch service, and I run the service in the foreground.
I have a notification showing the timer, that updates each second.
The notification stops updating 30 seconds after I leave the app, and I figured out that the cause is my device's battery optimization:
Inside my app's system settings, there is a battery optimization section, that contains a setting called Allow background activity, which can be on or off.
I found 2 threads (answer to thread 1 and answer to thread 2) trying to answer how to check the setting's state, and both of them suggest using ActivityManager.isBackgroundRestricted.
For me, it didn't work though. Whether the setting was switched On or Off, this function returned false.
How can I check if my app is allowed to run background activities?

Here's a photo of the setting in my phone (OnePlus 8t OxygenOS 12): system settings of my app

Nitzan Daloomy
  • 78
  • 4
  • 19
  • 1
    Unclear if you are aware of: https://dontkillmyapp.com/oneplus – Morrison Chang Apr 14 '22 at 03:36
  • @MorrisonChang Wasn't actually aware of all of these, but only some of them, and I didn't think it was mainly OnePlus's issue. After all, the device referred to in the first thread was a Samsung if I'm not mistaken. and this doesn't solve my problem, I need to know how to do all these steps programmatically, make my app suitable for any device out there, or at least most of them. – Nitzan Daloomy Apr 14 '22 at 09:41

1 Answers1

0

I see that it does not work for me also. So, you can see my two classes in my Git repo which I had also provided you within my previous answer. Also my repo link is here. Here you must see this in my AndroidManifest.xml and this method and this overridden method.

There I am asking for the ignore battery optimization permission and if it is enabled, it works fine or it if not enabled, asks for that permission.

Also, you can view the result in relation to this question's answer. This is the result


If that does not work, refer to this code:

String packageName = getPackageName();
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
    Toast.makeText(this, "no allowed", Toast.LENGTH_SHORT).show();
    // it is not enabled. Ask the user to do so from the settings.
}else {
    Toast.makeText(this, "allowed", Toast.LENGTH_SHORT).show();
    // good news! It works fine even in the background.
}

And to take the user to this setting's page, based on this answer:

Intent notificationIntent = new Intent(Settings.ACTION_APPLICATION_SETTINGS);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
String expandedNotificationText = String.format("Background activity is restricted on this device.\nPlease allow it so we can post an active notification during work sessions.\n\nTo do so, click on the notification to go to\nApp management -> search for %s -> Battery Usage -> enable 'Allow background activity')", getString(R.string.app_name));
notificationBuilder.setContentIntent(pendingIntent)
    .setContentText("Background activity is restricted on this device.")
    .setStyle(new NotificationCompat.BigTextStyle().bigText(expandedNotificationText))      
halfer
  • 19,471
  • 17
  • 87
  • 173
Sambhav. K
  • 3,046
  • 2
  • 5
  • 29
  • Hi, sorry for the long wait. It doesn't work for me ): – Nitzan Daloomy Apr 14 '22 at 13:41
  • @NitzanDaloomy Huh? That's surprising. Does it even ask for permission if not enabled? – Sambhav. K Apr 14 '22 at 14:26
  • @NitzanDaloomy Let me try on my one plus 9 r device. Maybe I encounter the issue there ! – Sambhav. K Apr 14 '22 at 14:29
  • @NitzanDaloomy Hmmm. Even I encounter that issue. But, let me check if the notification stops updating or not. But, the first issue which you told in your question is not seen. Ahaa. The second issue has come up now. Let me check it out. Also for me the timer stops running – Sambhav. K Apr 14 '22 at 14:38
  • WOWOWOWOWOWOWOWOW. I found it man. I will edit my post now. Just 5 mins @NitzanDaloomy – Sambhav. K Apr 14 '22 at 14:47
  • Yes!!!! that's it!! thank you so much (((: now I'll try to make an intent that redirects the user to this setting... do you have a clue how can I do that? – Nitzan Daloomy Apr 14 '22 at 16:30
  • No idea. I saw all the actions in the intent class and found nothing – Sambhav. K Apr 14 '22 at 16:35
  • Ok, I'll try to find it, thanks(: also, could you check my answer to the previous question? I've edited it because of some other issues that came up with the background-activities enabling. – Nitzan Daloomy Apr 14 '22 at 16:37
  • ok. sure. 5 mins – Sambhav. K Apr 14 '22 at 16:44
  • Hi! found the way to make an intent that redirects the user to this setting!! Intent intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS); it's taken from [this answer](https://stackoverflow.com/a/49098293/8099601), and it'd be best if you could add this to your answer. I'd suggest an edit to your answer. – Nitzan Daloomy Apr 14 '22 at 17:48
  • I did briefly. Should've I seen something in particular? – Nitzan Daloomy Apr 15 '22 at 23:46
  • @NitzanDaloomy No. Only abt the action buttons I have added for the notification – Sambhav. K Apr 16 '22 at 06:17
  • Yes. That is why I wanted u to see that. Run the entire app and see the magic – Sambhav. K Apr 16 '22 at 10:41
  • I've seen it running in the video you posted(: – Nitzan Daloomy Apr 16 '22 at 16:14
  • That's not the entire app. It does not have the action buttons and does not have a good ui. – Sambhav. K Apr 17 '22 at 04:07
  • Huh, I see I've already added buttons to my notification, and I try opening an activity from outside the app using the notification but I cant do it. I already added the right flag needed for this but still it doesn't work for me. I'll understand to figure out why – Nitzan Daloomy Apr 17 '22 at 10:56
  • Thats sus. It worked fine for me and it opened the activity also. Lets see that. – Sambhav. K Apr 18 '22 at 05:59
  • Please see [my question](https://stackoverflow.com/q/71911095/8099601), maybe you'll be able to help (: – Nitzan Daloomy Apr 18 '22 at 11:21