Android determines if the interface is now a home desktop method _android

Source: Internet
Author: User

The example in this article describes how Android can determine whether the interface is now a home desktop. Share to everyone for your reference. The implementation methods are as follows:

/** 
* Get the application package name that belongs to the desktop 
* @return return a list of strings containing all package names 
/private list<string> gethomes () { 
  list <String> names = new arraylist<string> (); 
  Packagemanager Packagemanager = This.getpackagemanager (); 
  Attribute 
  Intent Intent = new Intent (intent.action_main);
  Intent.addcategory (intent.category_home); 
  list<resolveinfo> Resolveinfo = packagemanager.queryintentactivities (Intent, 
    Packagemanager.match_ default_only); 
  for (Resolveinfo ri:resolveinfo) { 
   names.add (ri.activityInfo.packageName); 
   LOG.I ("Zhangyinfu Pinyinime.java", "PackageName =" + Ri.activityInfo.packageName);
  } 
  return names;
}

Analysis:

1 If you want to judge the current interface, it is necessary to judge the current Runningtasks first;

2) Introducing Activitymanager to obtain runningtasks;

3 Remove the PackageName of the topactivity in Runningtasks;

4 finally compared with the first step of the list to do!

5) need to add relevant permissions

Copy Code code as follows:
<uses-permission android:name= "Android.permission.GET_TASKS"/>

/** 
* To determine whether the current interface is desktop 
/public boolean ishome () { 
  Activitymanager Mactivitymanager = (Activitymanager) Getsystemservice (context.activity_service); 
  list<runningtaskinfo> RTI = mactivitymanager.getrunningtasks (1);
  List<string> STRs = Gethomes ();
  if (STRs!= null && strs.size () > 0) {return
   strs.contains (rti.get (0). Topactivity.getpackagename ());
  }else{return
   false;
  }

I hope this article will help you with your Android program.

Related Article

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.