如何停止运行服务?

问题描述:

我有这样的代码,描述服务:如何停止运行服务?

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; 
    } 
} 

我可以用

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

调用从活动这一服务,但我想调用它之后停止该运行中的服务,所以我可以称它为再次。我怎样才能做到这一点。谢谢

+0

相关http://*.com/questions/5555765/stop-service-in-android – 2016-02-14 10:26:32

还有另一种方法,它被称为stopService(Intent intent)。 服务内部,您可以拨打stopSelf()

使用活性stopService()方法:

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

stopService(Intent intent)是方法停止任何特定服务

呼叫stopService(新意图(YourActivity.this,ServiceClassName.class)); 查看更多关于服务的内容https://developer.android.com/guide/components/services.html