I have a service in my app which needs to be running in the background all the time. On all devices, it's working fine except Xiaomi. I have read somewhere that we need to enable auto-start in settings for an app to keep a service running.
So please tell me how to enable auto-start programmatically, because the user will never do that.
To give this method a try, open Settings and go to the Application Manager. It should be in "Installed Apps" or "Applications," depending on your device. Select an app from the list of downloaded apps and turn the Autostart option on or off.
You need to give permissions in the in build security application for xiaomi. 1. open the security app 2. go to permissions 3. go to auto start 4. enable the applications that you want to keep running in the background!
There's no way to find out whether the Auto-start option is enabled or not. You can manually check under Security permissions => Autostart => Enable Autostart .
The autostart permission allows apps to be started by receiving an implicit broadcast intent. This method consists of scheduling an implicit broadcast with AlarmManager, killing the app and checking if the broadcast caused it to respawn.
You cannot enable auto start directly, but you can redirect user to auto start setting screen and ask user to turn it on for your app. Use the below solution for xiaomi, oppo, vivo, letv, honor, asus, nokia, huawei phones. Autostart screen will be launched if it exists.
try { val intent = Intent() val manufacturer = Build.MANUFACTURER when { "xiaomi".equals(manufacturer, ignoreCase = true) -> { intent.component = ComponentName( "com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity" ) } "oppo".equals(manufacturer, ignoreCase = true) -> { intent.component = ComponentName( "com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity" ) } "vivo".equals(manufacturer, ignoreCase = true) -> { intent.component = ComponentName( "com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity" ) } "letv".equals(manufacturer, ignoreCase = true) -> { intent.component = ComponentName( "com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity" ) } "honor".equals(manufacturer, ignoreCase = true) -> { intent.component = ComponentName( "com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity" ) } "asus".equals(manufacturer, ignoreCase = true) -> { intent.component = ComponentName( "com.asus.mobilemanager", "com.asus.mobilemanager.powersaver.PowerSaverSettings" ) } "nokia".equals(manufacturer, ignoreCase = true) -> { intent.component = ComponentName( "com.evenwell.powersaving.g3", "com.evenwell.powersaving.g3.exception.PowerSaverExceptionActivity" ) } "huawei".equals(manufacturer, ignoreCase = true) -> { intent.component = ComponentName( "com.huawei.systemmanager", "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity" ) } } startActivity(intent) } catch (e: Exception) { /*Timber.e(e)*/ }
Quvonchbek Y answered
Try this...it's working for me. It will open the screen to enable autostart. But if you try to disable from there it will close the app. I am figuring out a solution for that. Till then you can use this as solution.
String manufacturer = "xiaomi"; if(manufacturer.equalsIgnoreCase(android.os.Build.MANUFACTURER)) { //this will open auto start screen where user can enable permission for your app Intent intent = new Intent(); intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity")); startActivity(intent); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With