androidpowermanagerandroid-powermanager

How to programmatically determine if an Android app is put to sleep on Samsung Galaxy?


Samsung Galaxy has the feature to put apps to sleep - which to my understanding is not the same as battery optimization for apps.

I need to programmatically check if an app is configured to sleep so I can properly warn the user about this and the fact that background tasks won't work.

I am aware of PowerManager.isIgnoringBatteryOptimizations() which however does not seem to be related to putting an app to sleep or not.


Solution

  • The thing with Samsung sleep feature is that it 'differs' from 'background optimizations'. Their feature is designed to freeze apps(just like freeze in the settings) after 3 days, but depending on user preference, it can be extended to 7 days. Currently, there is no known way to programmatically check if the app has been put to sleep.

    What you can do is to inform the user about this and ask to 'whitelist' the app from imminent sleeping.

    Intent intent = new Intent();
    intent.setComponent(new ComponentName("com.samsung.android.sm", "com.samsung.android.sm.ui.battery.BatteryActivity"));
    
    try {
       getActivity().startActivity(intent);
    } catch (ActivityNotFoundException exception) {
         //Manager not installed
    }
    

    Code source

    This code will try to open BatteryManager, from there, a user can select options to prevent from app sleeping.

    Here are the relevant questions:

    How do I check that my app enters optimizations Samsung devices?

    Samsung “App optimization” feature kills background applications after 3 days

    This blog should help you in figuring out the ways to inform the user about the issue