如何停止正在运行的服务? [英] How to stop running services?

查看:137
本文介绍了如何停止正在运行的服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个code,它描述了一个服务:

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

推荐答案

目前,它被称为stopService(意向意图)的另一种方法。 里面的服务,您可以拨打stopSelf()。

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

这篇关于如何停止正在运行的服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆