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";
if (manufacturer.equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
    //this will open auto start screen where user can enable permission for your app
    Intent intent1 = new Intent();
    intent1.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));

Also for Huawei:

if("huawei".equalsIgnoreCase(android.os.Build.MANUFACTURER) && !sp.getBoolean("protected",false)) {
        AlertDialog.Builder builder  = new AlertDialog.Builder(this);
                .setPositiveButton(R.string.go_to_protected, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent intent = new Intent();
                        intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity"));


To open app settings screen

Intent intent = new Intent();
Uri uri = Uri.fromParts("package", getPackageName(), null);
Leave a Reply

Your email address will not be published. Required fields are marked *


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