检查服务是运行在Android上? [英] Check if service is running on Android?
本文介绍了检查服务是运行在Android上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
→要检查服务是否运行
升写这篇code
l want to check if a service is running l wrote this code
public class startServiceOrNo {
public static void startServiceIfItsNotRuning(Class<?> class1, Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (class1.getName().equals(service.service.getClassName())) {
Lg.d("servisstart SERVICE ALREADY START" + class1.getName());
return;
}
}
Lg.d("servisstart SSERVICE NEW SERVIS " + class1.getName());
context.startService(new Intent(context, class1));
}
和使用它
startServiceOrNo.startServiceIfItsNotRuning(OfflineChopsMonitor.class,这一点)
and use it startServiceOrNo.startServiceIfItsNotRuning(OfflineChopsMonitor.class,this)
如果l检查服务从一类的工作,但如果从不同的L类检查同样的服务,其检查不工作
if l check service from one class its work, but if l check same service from different class, its check don't work
推荐答案
为什么你不让服务本身可以认识到这一点?
Why don't you let the Service itself figure that out?
public class MyService extends Service
{
private boolean mRunning;
@Override
public void onCreate()
{
super.onCreate();
mRunning = false;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
if (!mRunning) {
mRunning = true;
// do something
}
return super.onStartCommand(intent, flags, startId);
}
}
这篇关于检查服务是运行在Android上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文