How to Call Stopservice() Method of Service Class from the Calling Activity Class

Calling activity class method from Service class

Define an interface your Service will use to communicate events:

public interface ServiceCallbacks {
void doSomething();
}

Write your Service class. Your Activity will bind to this service, so follow the sample shown here. In addition, we will add a method to set the ServiceCallbacks.

public class MyService extends Service {
// Binder given to clients
private final IBinder binder = new LocalBinder();
// Registered callbacks
private ServiceCallbacks serviceCallbacks;

// Class used for the client Binder.
public class LocalBinder extends Binder {
MyService getService() {
// Return this instance of MyService so clients can call public methods
return MyService.this;
}
}

@Override
public IBinder onBind(Intent intent) {
return binder;
}

public void setCallbacks(ServiceCallbacks callbacks) {
serviceCallbacks = callbacks;
}
}

Write your Activity class following the same guide, but also make it implement your ServiceCallbacks interface. When you bind/unbind from the Service, you will register/unregister it by calling setCallbacks on the Service.

public class MyActivity extends Activity implements ServiceCallbacks {
private MyService myService;
private boolean bound = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(...);
}

@Override
protected void onStart() {
super.onStart();
// bind to Service
Intent intent = new Intent(this, MyService.class);
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}

@Override
protected void onStop() {
super.onStop();
// Unbind from service
if (bound) {
myService.setCallbacks(null); // unregister
unbindService(serviceConnection);
bound = false;
}
}

/** Callbacks for service binding, passed to bindService() */
private ServiceConnection serviceConnection = new ServiceConnection() {

@Override
public void onServiceConnected(ComponentName className, IBinder service) {
// cast the IBinder and get MyService instance
LocalBinder binder = (LocalBinder) service;
myService = binder.getService();
bound = true;
myService.setCallbacks(MyActivity.this); // register
}

@Override
public void onServiceDisconnected(ComponentName arg0) {
bound = false;
}
};

/* Defined by ServiceCallbacks interface */
@Override
public void doSomething() {
...
}
}

Now when your service wants to communicate back to the activity, just call one of the interface methods from earlier. Inside your service:

if (serviceCallbacks != null) { 
serviceCallbacks.doSomething();
}

stop service in android

onDestroyed()

is wrong name for

onDestroy()  

Did you make a mistake only in this question or in your code too?

how to call stopService from another Activity?

At first you have to unbind the service from the previous activity in onStop(). Otherwise you may met a window leaked exception.

Place the code to stop the service in the Application class.

public class AppController extends Application {
private static AppController mInstance;

public static synchronized AppController getInstance() {
return mInstance;
}
.........

public void stopService(View view) {
stopService(new Intent(getBaseContext(), MyService.class));
}
}

Then you can get the Application object from any class and you will be able to stop the service from any activity, but before that you have to unbind the service from the other activity in onStop().

To stop the service from anywhere, call:

AppController.getInstance().stopService();

how to force stop android service programmatically

Here's a simplified description of how to stop services :

stopSelf() is used to always stop the current service.

stopSelf(int startId) is also used to stop the current service, but only if startId was the ID specified the last time the service was started.

stopService(Intent service) is used to stop services, but from outside the service to be stopped.

visit this link for more details

please replace

return START_STICKY;

by

return START_NOT_STICKY;

Difference:

START_STICKY

the system will try to re-create your service after it is killed

START_NOT_STICKY

the system will not try to re-create your service after it is killed

Android - Stop a Service from a different class

Well, assuming you only want one instance of this service running at once you could hold a static variable in the service class and access it from anywhere. Example;

public class ToonService extends Service{

public static ToonService toonService;

public ToonService(){
toonService = this;
}
...

}

The constructor for ToonService now stores the created instance in the static variable toonService. Now you can access that service from anywhere from the class. Example below;

ToonService.toonService.stopSelf();

You could also handle multiple instances by having the class store a static List of running instances, rather than just the single instance. It is worth noting, that when you tell a service to stop, you are only requesting that it is stopped. Ultimately the Android OS will determine when it is closed.

Where to call stopService() to ensure a service is stopped?

See android service startService() and bindService() for a comprehensive answer.



Related Topics



Leave a reply



Submit