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

&lt;br&gt;<br>
@Override&lt;br&gt;<br>
public int onStartCommand(Intent intent, int flags, int startId) {&lt;br&gt;<br>
    return START_STICKY;&lt;br&gt;<br>
}&lt;br&gt;<br>

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

&lt;br&gt;<br>
String manufacturer = "xiaomi";&lt;br&gt;<br>
if (manufacturer.equalsIgnoreCase(android.os.Build.MANUFACTURER)) {&lt;br&gt;<br>
    //this will open auto start screen where user can enable permission for your app&lt;br&gt;<br>
    Intent intent1 = new Intent();&lt;br&gt;<br>
    intent1.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));&lt;br&gt;<br>
    startActivity(intent1);&lt;br&gt;<br>
}&lt;br&gt;<br>

Also for Huawei:

&lt;br&gt;<br>
if("huawei".equalsIgnoreCase(android.os.Build.MANUFACTURER) &amp;amp;&amp;amp; !sp.getBoolean("protected",false)) {&lt;br&gt;<br>
        AlertDialog.Builder builder  = new AlertDialog.Builder(this);&lt;br&gt;<br>
        builder.setTitle(R.string.huawei_headline).setMessage(R.string.huawei_text)&lt;br&gt;<br>
                .setPositiveButton(R.string.go_to_protected, new DialogInterface.OnClickListener() {&lt;br&gt;<br>
                    @Override&lt;br&gt;<br>
                    public void onClick(DialogInterface dialogInterface, int i) {&lt;br&gt;<br>
                        Intent intent = new Intent();&lt;br&gt;<br>
                        intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity"));&lt;br&gt;<br>
                        startActivity(intent);&lt;br&gt;<br>
                        sp.edit().putBoolean("protected",true).commit();&lt;br&gt;<br>
                    }&lt;br&gt;<br>
                }).create().show();&lt;br&gt;<br>
    }&lt;br&gt;<br>

Links:

https://stackoverflow.com/questions/41627537/service-not-start-again-after-kill-app-even-use-start-sticky-in-some-device

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

&lt;br&gt;<br>
Intent intent = new Intent();&lt;br&gt;<br>
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);&lt;br&gt;<br>
Uri uri = Uri.fromParts("package", getPackageName(), null);&lt;br&gt;<br>
intent.setData(uri);&lt;br&gt;<br>
startActivity(intent);&lt;br&gt;<br>

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]

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.