I am developing an applicaton which communicates with my service via receivers.
Service Code
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(LOCATION_UPDATE);
mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
receiverWorks(intent);
}
};
this.registerReceiver(mBroadcastReceiver, intentFilter);
return START_STICKY;
}
Service's Manifest declaration
<service
android:name="com.test.IService"
android:enabled="true"
android:label="IService"
android:process=":iservice" >
</service>
Problem is that it restarts itself after application (my application which uses this service) is being killed. How can i prevent this ? Tried to remove android:process but nothing changes.
Waiting for you help.
Thanks
If your Service is started by your app then actually your service is running on main process. so when app is killed service will also be stopped. So what you can do is, send broadcast from onTaskRemoved method of your service as follows: Intent intent = new Intent("com.
When Android decides to kill our app, our activities will call onDestroy method.
Call startService() method in Foreground Service Launcher class in your Activity to start a background service which will run forever. In order to sync something between application and server we can create a handler which run every time interval .
The service restarts itself because you are returning START_STICKY
in onStartCommand()
You need to return start_not_sticky
instead.
For more info, please read this: START_STICKY and START_NOT_STICKY
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With