Android ActivityManager 檢測Service與Activity運行狀態

在後臺與網絡交互中,須要使用Service來和服務器保持鏈接,所以Service在被進程Kill掉後,主動開啓Service是必要的,所以首先的檢測Service是否已啓動android

1.檢測Service是否已啓動

 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; 
     }

2.檢測應用App是否已啓動

權限服務器

<uses-permission android:name="android.permission.GET_TASKS"/>

代碼----判斷Activity是否正在運行網絡

ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> list = am.getRunningTasks(100);
boolean isAppRunning = false;
String MY_PKG_NAME = "com.cyberblue.iitag";
for (RunningTaskInfo info : list) {
	if (info.topActivity.getPackageName().equals(MY_PKG_NAME) || info.baseActivity.getPackageName().equals(MY_PKG_NAME)) {
		isAppRunning = true;
		Log.i(TAG,info.topActivity.getPackageName() + " info.baseActivity.getPackageName()="+info.baseActivity.getPackageName());
		break;
	}
}

3.檢測某Activity是否在當前Task的棧頂

/**
* 檢測某Activity是否在當前Task的棧頂
*/
public boolean isTopActivy(String cmdName){
        ActivityManager manager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
        List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);
        String cmpNameTemp = null;
        if(null != runningTaskInfos){
                cmpNameTemp=(runningTaskInfos.get(0).topActivity).toString);
                Log.e("cmpname","cmpname:"+cmpName);
        }
       if(null == cmpNameTemp)return false;
       return cmpNameTemp.equals(cmdName);
}

 

3.代碼檢測當前Activity是否是啓動器

    private  boolean isLauncherRunnig(Context context) {  
            boolean result = false ;  
            List<String> names = getAllTheLauncher();  
            ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE) ;  
            List<ActivityManager.RunningAppProcessInfo> appList = mActivityManager.getRunningAppProcesses() ;  
            for (RunningAppProcessInfo running : appList) {  
                    if (running.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {  
                            for (int i = 0; i < names.size(); i++) {  
                                    if (names.get(i).equals(running.processName)) {  
                                            result = true ;  
                                            break;  
                                    }  
                            }  
                    }  
            }  
            return result ;  
    }

4.判斷App是否正在運行

/**
	 * 判斷app是否正在運行
	 * @param ctx
	 * @param packageName
	 * @return
	 */
	public boolean appIsRunning(Context ctx,String packageName)
	{
		ActivityManager am = (ActivityManager) ctx.getSystemService(ACTIVITY_SERVICE);
		
		List<RunningAppProcessInfo> runningAppProcesses = am.getRunningAppProcesses();
		
		if(runningAppProcesses!=null)
		{
			for (RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) {
				
				if(runningAppProcessInfo.processName.startsWith(packageName))
				{
					return true;
				}
			}
		}
		
		return false;
	}
	/**
	 * app 是否在後臺運行
	 * @param ctx
	 * @param packageName
	 * @return
	 */
	public boolean appIsBackgroundRunning(Context ctx,String packageName)
	{
		ActivityManager am = (ActivityManager) ctx.getSystemService(ACTIVITY_SERVICE);
		
		List<RunningAppProcessInfo> runningAppProcesses = am.getRunningAppProcesses();
		
		if(runningAppProcesses!=null)
		{
			for (RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) {
				
				if(runningAppProcessInfo.processName.startsWith(packageName))
				{
					return runningAppProcessInfo.importance!=RunningAppProcessInfo.IMPORTANCE_FOREGROUND && runningAppProcessInfo.importance!=RunningAppProcessInfo.IMPORTANCE_VISIBLE; //排除無界面的app
				}
			}
		}
		
		return false;
	}
相關文章
相關標籤/搜索