Address: http://blog.csdn.net/you_and_me12/article/details/7284749
Recently I am working on my own applications. I need to use it to determine whether it is the current desktop or not.
I have referenced some articles, some of which have been mentioned but are not clear.
The code in this article has been put into practice and is for reference only!
1. First, obtain desktop applications (original system applications, go desktops, etc)
Analysis: 1) these applications will contain: "android. Intent. Category. Home ";
2) So you only need to find the package name of all the "android. Intent. Action. Main" activities declared as home!
The Code is as follows:
/*** Get the application package name of the desktop application * @ return returns a string list containing all Package Names */private list <string> gethomes () {list <string> names = new arraylist <string> (); packagemanager = This. getpackagemanager (); // property 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); system. out. println (Ri. activityinfo. packagename);} return names ;}
2. In your application, you need to determine whether it is a desktop (such as a service or a new thread)
Analysis: 1) to determine the current interface, determine the first one in the current runningtasks;
2) Introduce activitymanager to get runningtasks;
3) retrieve the packagename of the topactivity in runningtasks;
4) make a comparison with the list obtained in step 1!
5) Don't forget to add permissions.
<Uses-Permission Android: Name = "android. Permission. get_tasks"/>
The Code is as follows:
/*** Determine whether the current interface is desktop */Public Boolean ishome () {activitymanager mactivitymanager = (activitymanager) getsystemservice (context. activity_service); List <runningtaskinfo> RRTI = mactivitymanager. getrunningtasks (1); Return homepackagenames. contains (rrps. get (0 ). topactivity. getpackagename ());}
Note: The above code is for reference only. If necessary, it can be optimized or processed separately!