Check if service is running on Android? -
l want check if service running l wrote code
public class startserviceorno { public static void startserviceifitsnotruning(class<?> class1, context context) { activitymanager manager = (activitymanager) context.getsystemservice(context.activity_service); (runningserviceinfo service : manager.getrunningservices(integer.max_value)) { if (class1.getname().equals(service.service.getclassname())) { lg.d("servisstart service start" + class1.getname()); return; } } lg.d("servisstart sservice new servis " + class1.getname()); context.startservice(new intent(context, class1)); }
and use startserviceorno.startserviceifitsnotruning(offlinechopsmonitor.class,this)
if l check service 1 class work, if l check same service different class, check don't work
1.) in activity class:
private boolean ismyservicerunning(class<?> serviceclass) { activitymanager manager = (activitymanager) getsystemservice(context.activity_service); (runningserviceinfo service : manager.getrunningservices(integer.max_value)) { if (serviceclass.getname().equals(service.service.getclassname())) { return true; } } return false; }
2.) in non-activity class (broadcastreceiver):
private boolean ismyservicerunning(class<?> serviceclass,context context) { activitymanager manager = (activitymanager)context. getsystemservice(context.activity_service); (runningserviceinfo service : manager.getrunningservices(integer.max_value)) { if (serviceclass.getname().equals(service.service.getclassname())) { log.i("service already","running"); return true; } } log.i("service not","running"); return false; }
Comments
Post a Comment