如何获得参考运行服务? [英] How to get reference to running service?
本文介绍了如何获得参考运行服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一个一个问题 - 我的主要活动启动该服务,然后被封闭。当应用程序启动下一次应用程序应该参考这个服务并停止它。但我不知道我怎样才能到正在运行的服务的引用。拜托,我希望你能帮助我。谢谢。
解决方案
私人布尔isMyServiceRunning(){
ActivityManager经理=(ActivityManager)getSystemService(ACTIVITY_SERVICE);
对于(RunningServiceInfo服务:manager.getRunningServices(Integer.MAX_VALUE的)){
如果(your.service.classname.equals(service.service.getClassName())){
返回false;
}
}
返回true;
}如果(isMyServiceRunning()){
stopService(新意图(ServiceTest.this,MailService.class));
}
There is a one problem - my main activity starts the service and be closed then. When the application starts next time the application should get reference to this service and stop it. But I don't know how I can get a reference to a running service. Please, I hope you can help me. Thank you.
解决方案
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("your.service.classname".equals(service.service.getClassName())) {
return false;
}
}
return true;
}
if(isMyServiceRunning()) {
stopService(new Intent(ServiceTest.this,MailService.class));
}
这篇关于如何获得参考运行服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文