Service does not restart after a user kill a application on Xiaomi devices even we use
[code language="java"]
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
[/code]
Xiaomi devices have extra permissions called “auto start” that allows services be restarted automatically after they were killed.
I did not find how to check autostart permissions programmatically.
But we can redirect a user to permission screen by this code
[code language="java"]<br><br>
String manufacturer = "xiaomi";<br><br>
if (manufacturer.equalsIgnoreCase(android.os.Build.MANUFACTURER)) {<br><br>
//this will open auto start screen where user can enable permission for your app<br><br>
Intent intent1 = new Intent();<br><br>
intent1.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));<br><br>
startActivity(intent1);<br><br>
}<br><br>
[/code]
Also for Huawei:
[code language="java"]<br><br>
if("huawei".equalsIgnoreCase(android.os.Build.MANUFACTURER) && !sp.getBoolean("protected",false)) {<br><br>
AlertDialog.Builder builder = new AlertDialog.Builder(this);<br><br>
builder.setTitle(R.string.huawei_headline).setMessage(R.string.huawei_text)<br><br>
.setPositiveButton(R.string.go_to_protected, new DialogInterface.OnClickListener() {<br><br>
@Override<br><br>
public void onClick(DialogInterface dialogInterface, int i) {<br><br>
Intent intent = new Intent();<br><br>
intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity"));<br><br>
startActivity(intent);<br><br>
sp.edit().putBoolean("protected",true).commit();<br><br>
}<br><br>
}).create().show();<br><br>
}<br><br>
[/code]
Links:
https://stackoverflow.com/questions/42996646/how-to-enable-autostart-for-my-app-in-xiaomi-devices
http://en.miui.com/thread-410707-1-1.html
To open app settings screen
[code language="java"]<br><br>
Intent intent = new Intent();<br><br>
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);<br><br>
Uri uri = Uri.fromParts("package", getPackageName(), null);<br><br>
intent.setData(uri);<br><br>
startActivity(intent);<br><br>
[/code]
How to start Power Manager of all android manufactures to enable background and push notification?
[https://stackoverflow.com/questions/48166206/how-to-start-power-manager-of-all-android-manufactures-to-enable-background-and/48641229]
Collection of stock apps and mechanisms, which might affect background tasks and scheduled alarms
[https://github.com/dirkam/backgroundable-android]
Don’t kill my app
[https://dontkillmyapp.com/huawei]