简体   繁体   中英

How to Enable Autostart option programmatically in Samsung devices?

I am using the background services and it does not run on some devices as I have to enable the autostart permission for the app. I want to enable the autostart permission automatically where it needs. I am using this code but it redirects me on the window where the autostart permission is. But in samsung mobiles it is not directing to that window

  if ("xiaomi".equalsIgnoreCase(manufacturer)) {
                intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
                alertdialogue(intent);
            } else if ("oppo".equalsIgnoreCase(manufacturer)) {
                intent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity"));
                alertdialogue(intent);
            } else if ("vivo".equalsIgnoreCase(manufacturer)) {
                intent.setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
                alertdialogue(intent);
            } else if ("Letv".equalsIgnoreCase(manufacturer)) {
                intent.setComponent(new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity"));
                alertdialogue(intent);
            } else if ("Honor".equalsIgnoreCase(manufacturer)) {
                intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity"));
                alertdialogue(intent);
            }

i want solution for samsung devices

The other answers are right in saying that com.samsung.android.sm is the package name on N/7.0/24 and above, however I have found that on Q/10.0/29 the activity name has changed from com.samsung.android.sm.ui.battery.BatteryActivity to com.samsung.android.sm.battery.ui.BatteryActivity . I'm not sure about O or P. Best thing is to simply make a list of all the possible resolutions and try them in turn:

fun openSamsungBackgroundSettings() {

    val possibleIntents = mutableListOf<Intent>()
    val battery1 = "com.samsung.android.sm.ui.battery.BatteryActivity"
    val battery2 = "com.samsung.android.sm.battery.ui.BatteryActivity"
    val pkg = if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N)
        "com.samsung.android.lool"
    else
        "com.samsung.android.sm"
    possibleIntents.add(Intent().setComponent(ComponentName(pkg, battery1)))
    possibleIntents.add(Intent().setComponent(ComponentName(pkg, battery2)))
    //general settings as backup
    possibleIntents.add(Intent(Settings.ACTION_SETTINGS))
    possibleIntents.forEach {
        try {
            startActivity(it)
            return
        } catch (ex: Exception) {
            w(ex){"Failed to open intent:$it"}
        }
    }
}

new Intent().setComponent(new ComponentName("com.samsung.android.lool","com.samsung.android.sm.ui.battery.BatteryActivity"))

try this intent, but this is for taking to battery optimisation control activity, didnt found exact AutoStart intend for Samsung, if you find then let me know please

there is some Samsung mobile who don't support this authorization (dont have an activity for battery optimization) however for those who have there is two case

"com.samsung.android.sm", "com.samsung.android.sm.ui.battery.BatteryActivity" on Android L

"com.samsung.android.lool", "com.samsung.android.sm.ui.battery.BatteryActivity" on Android N

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