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.