In the development of the project, we often encounter some problems, in addition to setting up some activity of the startup mode, it is necessary to get this class has been started, to determine whether a class exists in the task stackthis time, we can use the following method:
/** * Determine if a class exists in the task stack * @return * /private Boolean isexsitmianactivity (class<?> cls) { Intent Intent = new Intent (this, CLS); ComponentName cmpname = intent.resolveactivity (Getpackagemanager ()); Boolean flag = false; if (cmpname! = null) {//indicates that the ACTIVITY exists in the system activitymanager am = (activitymanager) getsystemservice (activity_service ); list<runningtaskinfo> taskinfolist = Am.getrunningtasks (ten); for (Runningtaskinfo taskinfo:taskinfolist) { if (taskInfo.baseActivity.equals (cmpname)) {//indicates that it has started flag = true; break; Jump out of the loop, optimize efficiency }} } return flag; /** * for logic processing * /public void Dealwithintent () { if (isexsitmianactivity (Mainactivity.class)) {// There is this class/ /Operation }else{//does not exist for this class //Operation } }
In addition, there are:
determine whether an application exists based on the package name
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; }}
Second, determine whether the activity exists
Intent Intent = new Intent (); Intent.setclassname ("Package Name", "Class name");
Method One:
if (Getpackagemanager (). Resolveactivity (Intent, 0) = = null) { //indicates that the activity does not exist in the system }
Method Two:
if (Intent.resolveactivity (Getpackagemanager ()) = = null) { //indicates that the activity is not present in the system }
Method Three:
list<resolveinfo> list = Getpackagemanager (). Queryintentactivities (Intent, 0); if (list.size () = = 0) { //indicates that the activity is not present in the system }
isn't it very simple, huh? da.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android determines if a class exists in a task stack