android – How to stop running services?-ThrowExceptions

Exception or error:

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

How to solve:

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

###

use the activity stopService() method:

stopService(new Intent(ActivityName.this, ServiceClassName.class));

###

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

###

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

###

android.os.Process.killProcess(android.os.Process.myPid());

###

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

Leave a Reply

Your email address will not be published. Required fields are marked *