简体   繁体   中英

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.

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 sure which Xiaomi devices were tested)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM