20

I have this code that describes a service:

public class navigation_web extends Service {

    Context context;

    public void navigation() {
        Cursor mCur = getContentResolver().query(Browser.BOOKMARKS_URI,
            Browser.HISTORY_PROJECTION, null, null, null);
        mCur.moveToFirst();
        if (mCur.moveToFirst() && mCur.getCount() > 0) {
            while (mCur.isAfterLast() == false) {
                Log.v("titleIdx",
                    mCur.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX));
                Log.v("urlIdx",
                    mCur.getString(Browser.HISTORY_PROJECTION_URL_INDEX));
                mCur.moveToNext();
            }
        }
        // Browser.clearSearches(getContentResolver());
    }

    public void onCreate() {
        // Browser.clearHistory(getContentResolver());
        // Browser.clearSearches(getContentResolver());
        navigation();
        // super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
}

I can call this service from an activity using

startService(new Intent(Finalv2Activity.this, navigation_web.class));

But i want to stop this running service after calling it so i can call it again. How can i do this. thanks

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
emna
  • 423
  • 2
  • 6
  • 18

6 Answers6

43

There is another method that it's called stopService(Intent intent). Inside the service, you can call stopSelf().

Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
Simone Casagranda
  • 1,217
  • 17
  • 26
34

use the activity stopService() method:

stopService(new Intent(ActivityName.this, ServiceClassName.class));
Tal Kanel
  • 10,475
  • 10
  • 60
  • 98
4

stopService(Intent intent) is the method to stop any specific service

Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
Ishu
  • 5,357
  • 4
  • 16
  • 17
1
android.os.Process.killProcess(android.os.Process.myPid());
Makvin
  • 3,475
  • 27
  • 26
  • What if the system restarts the service if I kill its PID? `Scheduling restart of crashed service [service] in 1000ms`. I'm having some problems since I've put services on separate processes to be able to kill them by force (using the PID) and I think this is the problem itself. But I don't know how to stop it from restarting. And I have START_NOT_STICKY on it. Would you know what to do here? – Edw590 Jan 04 '21 at 10:26
1

Complementing @Makvin answer. Stopping a service outside of it

public static ActivityManager.RunningServiceInfo getRunningServiceInfo(Class serviceClass, Context context) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            return service;
        }
    }
    return null;
}

And after

new Timer().schedule(new TimerTask() {
        @Override
        public void run() {
            final ActivityManager.RunningServiceInfo serviceInfo = getRunningServiceInfo(service, c);
            if (serviceInfo != null) {
                android.os.Process.killProcess(serviceInfo.pid); //Stop the running service
            }
        }
}, 200); 

I'm putting killProcess inside another thread otherwise for some reason all the app is killed. In this way the app is minimized when the process is killed (less bad), suggestions are welcome

Rodrigo João Bertotti
  • 5,179
  • 2
  • 23
  • 34
  • I believe you need to put the service you want to kill in a separate process. Like this: https://stackoverflow.com/a/38318003/8228163. I just did this and this started working!! It was killing the PID of the app itself (I didn't put it inside any Threads), which was the same as the service's PID, so I went looking and found we can put stuff in different processes, which helps a lot (I thought this was already in different processes). And this led me to it which just solved mt problem, so thank you!!! – Edw590 Oct 01 '20 at 16:08
0

call
stopService(new Intent(YourActivity.this, ServiceClassName.class));
see more about services in https://developer.android.com/guide/components/services.html

SAKhan
  • 249
  • 4
  • 16