Domanda

In my android app i already have created a service which is getting data on a background thread and sending it to the server regularly. I have three category of users which has been set by preference Activity, Now i want that My service should only run if and only if a particular type of user had been chosen, else service should get stop. So i want to stop the service on the change of my sharedpreference (listpreference in the xml file). How can i retrieve the reference of my service (which is running in the background not a new reference) and bind to stop when user change the value in listpreference.

Thanks in advance.

È stato utile?

Soluzione

You don't need to bind to it in order to stop it. Wherever you have a Context reference (Activity, Service or base application context) just call stopService():

private void stopServiceUsingContext(Context context) {
    context.stopService(new Intent(context, MyServiceClass.class));
}

EDIT: If you want to stop the service in response to a preference change listener, then you should have a reference to an Activity which extends Context so above code applies

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top