1

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
  • 166
  • 5
  • 24
  • 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,824
  • 17
  • 99
  • 186
Sambhav Khandelwal
  • 3,585
  • 2
  • 7
  • 38