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!