Android 如何檢測一個服務是否還在運行?

public static  boolean isServiceStarted(Context context,String PackageName){
    boolean isStarted =false;


    try{
    int intGetTastCounter = 1000;


    ActivityManager mActivityManager =
        (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);


    List<ActivityManager.RunningServiceInfo> mRunningService = 
        mActivityManager.getRunningServices(intGetTastCounter);


    for (ActivityManager.RunningServiceInfo amService : mRunningService){
        if(0 == amService.service.getPackageName().compareTo(PackageName)){
        isStarted = true;
        break;
        }
    }
    }catch(SecurityException e){
    e.printStackTrace();
    }            


    return isStarted;                
}

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *