2009-12-11 98 views
36

我在一项活动中开始一项服务,然后我希望服务一段时间后自行停止。如何自行停止服务?

我在服务中调用了stopSelf(),但它并没有工作。

如何让服务自行停止?谢谢!!!

+7

因为答案是“叫stopSelf()”,你将需要提供关于更多的细节“它好好尝试一下工作”。 – CommonsWare 2009-12-11 10:18:02

+0

需要更多的内容,只是'“不工作”' – footy 2011-11-26 16:13:00

+1

像一个答案?随意标记*接受* :) – 2013-01-20 12:04:55

回答

4

如果通过“不工作”,你的意思是这个过程不会被杀死,那么这就是android的工作原理。 System.exit(0)Process.killProcess(Process.myPid())会杀死你的进程。但这不是Android做事的方式。

HTH

51

通过说“不行”,我猜你的意思是该服务的onDestroy() - 方法不会被调用。

我有同样的问题,因为我bound一些ServiceConnection服务本身使用标志BIND_AUTO_CREATE。 这会导致服务保持活动状态,直到每个连接为unbound

一旦我改变使用没有标志(零),我没有问题自己杀死服务(stopSelf())。

示例代码:

final Context appContext = context.getApplicationContext(); 
final Intent intent = new Intent(appContext, MusicService.class); 
appContext.startService(intent); 
ServiceConnection connection = new ServiceConnection() { 
    // ... 
}; 
appContext.bindService(intent, connection, 0); 

杀害该服务(未处理):

this.stopSelf(); 

希望有所帮助。

5

,因为你没有发布你的代码,我不能确切地知道你在做什么,但你必须声明你正在停止:

this.stopSelf(); 

为:

public class BatchUploadGpsData extends Service { 
    @Override 
    public void onCreate() { 
     Log.d("testingStopSelf", "here i am, rockin like a hurricane. onCreate service"); 
     this.stopSelf(); 
    } 
+3

纠正我,如果我错了,但不会停止自我()没有“这个”的工作,以及? – 2015-03-21 20:17:26

+0

@AbrahamPhilip'This'是明确的,但我不能说这是绝对必要的。请尽可能测试。韩国社交协会。 – 2015-03-25 23:46:22

+1

这不是必须的,但为了清晰起见添加它并没有什么坏处 – 2015-07-20 10:37:01

14

通过调用stopSelf(),服务停止。

请确保没有线程在后台运行,这会让您觉得服务尚未停止。

在您的线程中添加打印语句。

希望这会有所帮助。

0

我刚碰到同样的问题。就我而言,我有一个单身服务经理,用于与服务进行通信。在管理服务已启动这样的:

context.bindService(new Intent(context, MyService.class), serviceConnection, Context.BIND_AUTO_CREATE); 

通过由阿利克Elzin作为建议删除Context.BIND_AUTO_CREATE,我已经能够使用this.stopSelf()停止服务,并拥有的onDestroy()称为当这样做时。这个问题是,之后我无法使用上面的命令从管理器重新启动服务。

最后,我已经通过使用服务的回调来解决这个问题,该服务告诉经理停止服务。这种方式经理总是负责开始/停止服务,一切似乎都正常工作。我不知道这样做是否有任何反指标。

代码非常简单。在服务创建一个回调并在连接类设置在这样的管理者:

private ServiceConnection mServiceConnection = new ServiceConnection() { 
    public void onServiceConnected(ComponentName className, IBinder service) { 
     myService = ((MyService.LocalBinder)service).getService(); 

     myService.setCallback(new MyService.MyServiceCallback() { 
      @Override 
      public void onStop() { 
       stopService(); 
      } 
     }); 
    } 

    public void onServiceDisconnected(ComponentName className) { 
     myService = null; 
    } 
}; 

和停止服务:

public void stopService() 
{ 
    if(mServiceConnection != null){ 
     try { 
      mContext.unbindService(mServiceConnection); 
     } catch (Exception e) {} 
    } 

    mContext.stopService(new Intent(mContext, BleDiscoveryService.class)); 
} 

在服务,只需拨打myCallback.onStop()当您需要阻止它。

2
stopForeground(true); 
      stopSelf(); 
0

这里没有提到的另一个肮脏的黑客是抛出像NPE的异常。有一天我需要停止InputMethodService,并且这个入侵很有用。

0

为了让你的服​​务停止本身..创建BroadcastReceiver类。在您的服务调用你的接收器这样的..

在服务

sendBroadcast(new Intent("MyReceiver")); 

在广播接收机

public class MyReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     context.stopService(new Intent(context,NotificationService.class)); 
    } 
} 

Manifest f ILE

<receiver 
     android:name="MyReceiver" 
     android:enabled="true" 
     android:exported="true"> 
     <intent-filter> 
      <action android:name="MyReceiver"/> 
     </intent-filter> 
    </receiver> 
0

,如果你在你的服务使用单独的Thread,通过调用stopSelf()stopService()Thread保持运行停止售后服务。如果u要停止Threadü应该调用在ThreadThread.interrupted()(这可能会导致Exception