I have service in my app which need to be running in background all the time , in all devices its working fine except Xiaomi , i have read some where that we need to enable auto start in settings for app to keep service running.
So please tell me how o enable auto start pro grammatically because user will never do that.
Any help will be appreciated.
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 and vivo phones. Autostart screen will be launched if it exists.
try {
Intent intent = new Intent();
String manufacturer = android.os.Build.MANUFACTURER;
if ("xiaomi".equalsIgnoreCase(manufacturer)) {
intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
} else if ("oppo".equalsIgnoreCase(manufacturer)) {
intent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity"));
} else if ("vivo".equalsIgnoreCase(manufacturer)) {
intent.setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
}
List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if (list.size() > 0) {
context.startActivity(intent);
}
} catch (Exception e) {
Crashlytics.logException(e);
}
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);
}
You may try this:
if ("xiaomi".equalsIgnoreCase(str))
{
intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
}
else if ("oppo".equalsIgnoreCase(str))
{
intent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity"));
}
else if ("vivo".equalsIgnoreCase(str))
{
intent.setComponent(new ComponentName("com.iqoo.secure", "com.iqoo.secure.MainGuideActivity."));
}
you can do it by following:
if (manufactXiaomi.equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
if (!session.getVisibilityOfAutoStartDialog()) {Intent intent = new Intent();
intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
startActivity(intent);}}
if you want to keep running your service is the background you need to change some setting of your device Check This
might above code works for you
I stumbled upon this library.
Autostarter. It is a autostarter library for different device manufacturers
Last time i used it, it had support for Xiaomi and Letv devices. I cant really give you code examples, but i hope it helps someone who stumbles upon this