Hi i am running a background service using alaram manager its working fine but for some mi devices background service is not working.I used sevices but it is not working how to run my background service in mi ?
3 Answers
MI UI has its own security options, so what you need to is not just above mentioned sticky Service, you need to
- Enable Autostart
- go to power setting make changes as per these youtube videos https://www.youtube.com/watch?v=-Ffgir-QgsU, or refer for this for more suggestions https://www.quora.com/How-do-I-keep-an-app-running-in-the-background-in-MIUI
- then you have created a custom broadcast receiver which will start the service when your service is destroyed as per this example https://fabcirablog.weebly.com/blog/creating-a-never-ending-background-service-in-android
If the 3rd option doesn't work onDestroy recall of the service call the custom broadcast receiver on w
public void onTaskRemoved(Intent rootIntent) { super.onTaskRemoved(rootIntent); Log.e(TAG, " In recieve Custome Broadcast receiver"); Intent broadcastIntent = new Intent("ac.in.ActivityRecognition.RestartSensor"); sendBroadcast(broadcastIntent);
}

- 754
- 1
- 7
- 12
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.android.settings","com.android.settings.Settings$HighPowerApplicationsActivity"));
startActivity(intent);
try this code
- it will open one setting page
- then find your app and then tap "Don't Optimize"
- it will turn off battery optimization and your background services run without problem

- 372
- 5
- 21
When you start a service by extending an Service class than you will get the call inside OnStartCommand() this method has three types of return type on the basis of this return type operating system itself manage when to start a service. So suppose if your service gets killed in between due to low memory or any other issue when you return a correct type from onStartCommand() than os will take care of when to start service again.
Three return types are:
START_STICKY : When this is the return type than os takes the guarantee to restart the service again if its get killed it will definitely start you service again even if there is no pending intent it will start the service by passing intent as null.
START_NOT_STICKY: says that, after returning from onStartCreated(), if the process is killed with no remaining start commands to deliver, then the service will be stopped instead of restarted. This makes a lot more sense for services that are intended to only run while executing commands sent to them. For example, a service may be started every 15 minutes from an alarm to poll some network state. If it gets killed while doing that work, it would be best to just let it be stopped and get started the next time the alarm fires.
START_REDELIVER_INTENT is like START_NOT_STICKY, except if the service's process is killed before it calls stopSelf() for a given intent, that intent will be re-delivered to it until it completes (unless after some number of more tries it still can't complete, at which point the system gives up). This is useful for services that are receiving commands of work to do, and want to make sure they do eventually complete the work for each command sent.

- 150
- 1
- 7