Create A Android App Service Which Run Even After App Is Terminated
Solution 1:
See onStartCommand
method and the param:
START_STICKY
START_NOT_STICKY
START_REDELIVER_INTENT
START_STICKY_COMPATIBILITY
If doesn't work, Maybe you have no system permission to keep the service to run in the background
Solution 2:
Here is demo :
publicclassSyncJobServiceextendsService {
privateStringTAG = "mytag";
privateTimer timer = newTimer();
privateEventBus evenBus = EventBus.getDefault();
publicstaticDate lastSyncDate;
@Overridepublic IBinder onBind(Intent intent) {
returnnull;
}
@OverridepublicvoidonCreate() {
super.onCreate();
timer.scheduleAtFixedRate(newTimerTask() {
@Overridepublicvoidrun() {
Log.d(TAG, "run: StartSyncEvent");
// Implement your logic
}
}, 0, 5 * 60 * 1000);//5 Minutes
}
@OverridepublicvoidonDestroy() {
Log.d(TAG, "onDestroy: ");
super.onDestroy();
}
}
ManiFest file :
<serviceandroid:name=".service.SyncJobService"android:enabled="true"android:exported="false" />
Solution 3:
In some phones like lenovo,xolo etc are terminate background third party application. So , it will not allow service in background when the application is closed.Check your service with other phones. 1.Better Use Start foreground service . 2.In start sticky,if the application is terminated, service automatically restarted for background process.
Solution 4:
Make your service as foreground process so that the Android system will treat as foreground app and will not kill that easily. Use the below code to make your service as foreground.
StartForeground(100, new Notification ());
Remember you need to call stopforeground(100) in ondestroy of service.
Solution 5:
I found the solution finally...
http://fabcirablog.weebly.com/blog/creating-a-never-ending-background-service-in-android
Post a Comment for "Create A Android App Service Which Run Even After App Is Terminated"