Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android service restarts itself on application killed

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

like image 906
dracula Avatar asked Aug 21 '13 14:08

dracula


People also ask

How do I keep Android service running when an app is killed?

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.

Which method is called when app is killed android?

When Android decides to kill our app, our activities will call onDestroy method.

How do I run service forever on Android?

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 .


1 Answers

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

like image 65
Shrikant Ballal Avatar answered Sep 27 '22 19:09

Shrikant Ballal