I have an app which will start a background service , which works fine on all android phones and emulators except in Redmi 1s phones. Service starts normally. But when user clears out the app from recent apps interface, all the services associated with my app getting killed. My app needs this background service to be running. Is there anythinng I can do to prevent this issue?
2 Answers
Try this:when you are in task manager (long press Home button), drag down the app you want to prevent from killing and lock it into memory. You will notice a small lock icon on a corner of app icon.

- 708
- 7
- 16
-
1Thats something I 've to tell my users. Is there anything , I (as an app developer) could do? – user1055979 Jan 20 '15 at 13:02
I know i am late in answering this question but in any case if someone is still looking for the solution in this case, this answer may be helpful for themm. I am not very sure about this specific device as this device is not available with me, but as far as this issue is concerned with Xiaomi devices, i am using Autostart permission for restarting the service once it is killed from the task manager.
You can ask your users to allow the autostart permission manually and as long as this permission is allowed for your app, it will restart your service every time you remove it from the task manager.
Steps to ask the autostart permission from the user:
Redirect user to the Autostart settings page if the device manufacturer is Xiaomi.
if ("xiaomi".equalsIgnoreCase(android.os.Build.MANUFACTURER)) { Intent autostartIntent = new Intent(); autostartIntent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity")); startActivity(autostartIntent); }
- Here your user will have to turn on Autostart for your app.
Additionally you need to send a broadcast from onDestroy() of your service which will restart your service everytime it is being destroyed.
onDestroy() of Service class
@Override
public void onDestroy() {
super.onDestroy();
Log.i("EXIT", "ondestroy!");
Intent broadcastIntent = new Intent("ac.in.ActivityRecognition.RestartSensor");
sendBroadcast(broadcastIntent);
}
Define a Receiver in your Manifest file
<receiver
android:name=".recievers.SensorRestarterBroadcastReceiver"
android:enabled="true"
android:exported="true"
android:label="RestartServiceWhenStopped">
<intent-filter>
<action android:name="ac.in.ActivityRecognition.RestartSensor" />
</intent-filter>
</receiver>
CustomBroadcastReceiver.java
public class SensorRestarterBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(SensorRestarterBroadcastReceiver.class.getSimpleName(), "Service Stopped!");
context.startService(new Intent(context, MyService.class)); // Restart your service here
}
}
Once you are done with the above mentioned steps you will be able to restart your service even after your app is killed from the task manager.

- 412
- 5
- 21
-
This is super helpful. Also [here](https://stackoverflow.com/questions/39366231/how-to-check-miui-autostart-permission-programmatically), find how to check for the permission: – mc51 Mar 18 '20 at 21:51