文章

Android判断Service是否存在和Activity是否存在

文章目录

备忘在这,防止下次需要。

1、判断服务是否在运行

	/**
	 * app service running
	 */
	public static boolean appServiceIsRunning(String classname) {
		if (null == classname) {
			Log.e("------------classname NULL---------------");
			return false;
		}
		ActivityManager activityManager = (ActivityManager) mContext
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningServiceInfo> servicelist = activityManager
				.getRunningServices(50);
		if (0 > servicelist.size()) {
			return false;
		}
		for (int i = 0; i < servicelist.size(); i++) {
			if (null != servicelist.get(i)
					&& null != servicelist.get(i).service
					&& classname.equals(servicelist.get(i).service
							.getClassName())) {
				return true;
			}
		}
		return false;
	}

2、检测Activity是否存在


	/**
	 * activity exits
	 */

	public static boolean appActivityExist(Intent intent) {

		List<ResolveInfo> activitylist = mContext.getPackageManager()
				.queryIntentActivities(intent,
						PackageManager.MATCH_DEFAULT_ONLY);
		if (activitylist.size() > 0) {
			return true;
		}
	
		return false;
	}

 

发表评论