问题
This is service which I wanted to run in the background
public class CustomMyService extends Service {
public CustomMyService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Intent intent = new Intent("com.android.ServiceStopped");
sendBroadcast(intent);
}
}
Manifest File
<service android:name=".CustomMyService">
<intent-filter android:priority="1000">
<action android:name="android.location.PROVIDERS_CHANGED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</service>
Please kindly someone let me know. I did all google and youtube search, nothing is working.
Im using redmi note 3, in which I have a option called autostart, if I give permission to this app for autostart, service runs in background.
But this option is not there in many of other android smart phone, so app services will be killed when app is terminated.
Please kindly let me know how can service be running, even after app is terminated.
回答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
回答2:
Use AlarmManager to envoke your service time to time. Because service will be stops when app is terminated.it's need to revoke again and Alarm manage helps to start your service again.
There are many methods mention for continuous service ,but those methods are not helpful after kitkat version.
If you have got success to run your service continuously even after app is terminated,without use of any Alarm Manager like scheduling methods please let me know.
回答3:
Here is demo :
public class SyncJobService extends Service {
private String TAG = "mytag";
private Timer timer = new Timer();
private EventBus evenBus = EventBus.getDefault();
public static Date lastSyncDate;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Log.d(TAG, "run: StartSyncEvent");
// Implement your logic
}
}, 0, 5 * 60 * 1000);//5 Minutes
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy: ");
super.onDestroy();
}
}
ManiFest file :
<service
android:name=".service.SyncJobService"
android:enabled="true"
android:exported="false" />
回答4:
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.
回答5:
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.
回答6:
I found the solution finally...
http://fabcirablog.weebly.com/blog/creating-a-never-ending-background-service-in-android
来源:https://stackoverflow.com/questions/42592897/create-a-android-app-service-which-run-even-after-app-is-terminated