标签:
/** * 判断某一个类是否存在任务栈里面 * @return */ private boolean isExsitMianActivity(Class<?> cls){ Intent intent = new Intent(this, cls); ComponentName cmpName = intent.resolveActivity(getPackageManager()); boolean flag = false; if (cmpName != null) { // 说明系统中存在这个activity ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE); List<RunningTaskInfo> taskInfoList = am.getRunningTasks(10); for (RunningTaskInfo taskInfo : taskInfoList) { if (taskInfo.baseActivity.equals(cmpName)) { // 说明它已经启动了 flag = true; break; //跳出循环,优化效率 } } } return flag; } /** * 进行逻辑处理 */ public void dealWithIntent(){ if(isExsitMianActivity(MainActivity.class)){//存在这个类 //进行操作 }else{//不存在这个类 //进行操作 } }
public boolean checkApplication(String packageName) { if (packageName == null || "".equals(packageName)){ return false; } try { ApplicationInfo info = getPackageManager().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES); return true; } catch (NameNotFoundException e) { return false; } }
二、判断Activity是否存在
Intent intent = new Intent(); intent.setClassName("包名", "类名");
方法一:
if (getPackageManager().resolveActivity(intent, 0) == null) { // 说明系统中不存在这个activity }
方法二:
if(intent.resolveActivity(getPackageManager()) == null) { // 说明系统中不存在这个activity }
方法三:
List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, 0); if (list.size() == 0) { // 说明系统中不存在这个activity }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/androidstarjack/article/details/46708243