'Send user to Security Boost speed settings

i am developing an android application an, in order to avoid that the os kill my background service, i realized that the app must to be locked. User could lock the app from recent aplications or by settings menu/security/Boost speed/Lock apps list (My test phone has MIUI 12.5.8 version - Andorid 11).

Are any way to send user to this settings menu screen by code?? The component name of these settings is knowed?

On the other hand, are any way to check if an aplication is locked by code?

I hope somebody have faced same issu in past and could respond my answer.



Solution 1:[1]

check out battery optimization settings, but this a default Android way for setting some restriction to apps. Some manufacturers are tweaking this feature by changing its behavior or adding more restrictions and there is no guarantee that below snippets will open this custom restricting feature instead of Android OS default one (or anything)

public static boolean isBatteryOptimizationEnabled(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        return pm != null && !pm.isIgnoringBatteryOptimizations(context.getPackageName());
    }
    return false;
}

@SuppressLint("BatteryLife")
@TargetApi(23)
public static void goToBatteryOptimizationSettings(Context context) {
    Intent intent = new Intent();
    String packageName = context.getPackageName();
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    if (pm == null || pm.isIgnoringBatteryOptimizations(packageName))
        intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
    else {
        intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
        intent.setData(Uri.parse("package:" + packageName));
    }
    context.startActivity(intent);
}

still, disabling battery optimization in above way prevent my ForegroundService being killed after some time, it works on tested devices for days and weeks (but not shure which Xiaomi devices were tested)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 snachmsm