Posted on Leave a comment

Restart service if start_sticky on Xiaomi

Service does not restart after a user kill a application on Xiaomi devices even we use

public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;

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

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>

Also for Huawei:

if("huawei".equalsIgnoreCase(android.os.Build.MANUFACTURER) && !sp.getBoolean("protected",false)) {<br><br>
        AlertDialog.Builder builder  = new AlertDialog.Builder(this);<br><br>
                .setPositiveButton(R.string.go_to_protected, new DialogInterface.OnClickListener() {<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>


To open app settings screen

Intent intent = new Intent();<br><br>
Uri uri = Uri.fromParts("package", getPackageName(), null);<br><br>

How to start Power Manager of all android manufactures to enable background and push notification?

Collection of stock apps and mechanisms, which might affect background tasks and scheduled alarms

Don’t kill my app

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.