Android determines if a class exists in a task stack

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.