How to stop running services?

*爱你&永不变心* 提交于 2019-12-18 02:03:51

问题


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


回答1:


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




回答2:


use the activity stopService() method:

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



回答3:


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




回答4:


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



回答5:


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




回答6:


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



来源:https://stackoverflow.com/questions/9964356/how-to-stop-running-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!