Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start a android service from one activity and stop service in another activity?

I need to start an android service from activity and stop the activity and stop service from other activity.

any help will be appreciated

like image 764
RanjitRock Avatar asked Sep 17 '11 11:09

RanjitRock


1 Answers

A service skeleton:

public class MyService extends Service {
    public static final String TAG = "MyServiceTag";
    ...
}

This is part of the starting activity:

processStartService(MyService.TAG);

private void processStartService(final String tag) {
    Intent intent = new Intent(getApplicationContext(), MyService.class);
    intent.addCategory(tag);
    startService(intent);
}

This is part of the stopping activity:

processStopService(MyService.TAG);

private void processStopService(final String tag) {
    Intent intent = new Intent(getApplicationContext(), MyService.class);
    intent.addCategory(tag);
    stopService(intent);
}
like image 76
Harald Wilhelm Avatar answered Nov 04 '22 00:11

Harald Wilhelm