一、
private boolean isMyServiceRunning(){
ActivityManager manager= (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for(RunningServiceInfo service: manager.getRunningServices(Integer.MAX_VALUE))
{
if(MyService.class.getName().equals(service.service.getClassName()))
{
returntrue;
}
}
returnfalse;
}
二、啟動服務必須用startserver()
Intent bindIntent = new Intent(this,ServiceTask.class);
startService(bindIntent);
bindService(bindIntent,mConnection,0);
public class ServiceTools {
private static String LOG_TAG = ServiceTools.class.getName();
public static boolean isServiceRunning(String serviceClassName)
{
final ActivityManager activityManager = (ActivityManager)Application.getContext().getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);
for (RunningServiceInfo runningServiceInfo : services)
{
if (runningServiceInfo.service.getClassName().equals(serviceClassName))
{
return true;
}
}
return false;
}
}