Android determines whether the current interface is on the desktop

Source: Internet
Author: User

Zookeeper

In mobile development, it is often necessary to determine whether the current interface is a desktop. There are many examples on the Internet. I will post them here to facilitate future queries, second, it is convenient for the code friends who need it.
On Android devices, it is generally based on the following principle to determine whether the current interface desktop is based on: android desktop is actually composed of many applications, which can also be called desktop applications. After knowing this, the problem will be simple, first, we only need to get the package names of all the desktop applications, and then get the package names of the currently running applications, if the package name of the currently running application is one of the desktop application package names, the interface is on the desktop; otherwise, no.
The following code is provided:

/*** Get the application package name of the desktop application * @ return returns a string list containing all Package Names */public static ArrayList
 
  
GetHomePackages (Context context) {ArrayList
  
   
Names = new ArrayList
   
    
(); PackageManager packageManager = context. getPackageManager (); // attribute Intent intent = new Intent (Intent. ACTION_MAIN); intent. addCategory (Intent. CATEGORY_HOME); List
    
     
ResolveInfo = packageManager. queryIntentActivities (intent, PackageManager. MATCH_DEFAULT_ONLY); for (ResolveInfo ri: resolveInfo) {names. add (ri. activityInfo. packageName);} return names;}/*** determine whether the current interface is a desktop */public static boolean isHome (Context context) {ActivityManager mActivityManager = (ActivityManager) context. getSystemService (Context. ACTIVITY_SERVICE); List
     
      
RRTI = mActivityManager. getRunningTasks (1); String runningPackage = rrps. get (0). topActivity. getPackageName (); ArrayList
      
        Export topapppackages = getHomePackages (context); // for (int I = 0; I <shorttopapppackages. size (); I ++) {// Log. e ("desktop package name", using topapppackages. get (I) + "-----"); //} Log. e ("first", rrps. get (0 ). topActivity. getPackageName (); return response topapppackages. contains (runningPackage );}
      
     
    
   
  
 
Now, based on the two methods, you can determine whether the current interface is on the desktop. If there are other methods, please share them with us!

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.