我已经启动了我的服务在我的一个主要活动class.The问题是,当我的应用程序不运行时,我想停止我的service.How是否可以实现this.Is有任何选项来停止它,而不停止它使用任何按钮单击或控件的其他单击事件。
另外,当我的应用程序正在运行时,是否有任何方法可以指示服务是否正在运行
发布于 2011-09-30 19:41:51
只需像这样触发一个意图:
Intent intent = new Intent(this, Your_service.class);只需在您停止服务的地方添加以下代码块:
stopService(intent);Update:
将此方法添加到应用程序的每个活动中
public boolean onKeyDown(int key, KeyEvent event)
{
switch(key.getAction())
{
case KeyEvent.KEYCODE_HOME :
Intent intent = new Intent(this, Your_service.class);
stopService(intent);
break;
}
return true;
}发布于 2017-12-05 19:20:32
使用intent可以停止服务。
Intent intentStartervice = new Intent(context, MyService.class);
stopService(intentStartService);或
只要传递上下文即可。
context.stopService(new Intent(context, MyService.class));发布于 2011-09-30 19:27:17
您可以在launcher activity的onCreate中启动服务,也可以在同一个activity的onStop中停止服务。您可以参考以下内容获取完整的服务教程:
http://marakana.com/forums/android/examples/60.html
是的,最好使用相同的intent变量来启动和停止activity.At,不要忘记将您的服务添加到清单中。
https://stackoverflow.com/questions/7609638
复制相似问题