In Android, how does one determine whether the current interface is home main desktop?

Source: Internet
Author: User

/**
* Obtain the application package name of the desktop application.
* @ Return returns the string list containing all package names.
*/
Private list <string> gethomes (){
List <string> names = new arraylist <string> ();
Packagemanager = This. getpackagemanager ();
// Attributes
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) 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) related permissions must be added.

<Uses-Permission Android: Name = "android. Permission. get_tasks"/>

/**
* Determine whether the current interface is a desktop
*/
Public Boolean ishome (){
Activitymanager mactivitymanager = (activitymanager) getsystemservice (context. activity_service );

List <runningtaskinfo> RRTI = mactivitymanager. getrunningtasks (1 );
List <string> STRs = gethomes ();
If (STRs! = NULL & STRs. Size ()> 0 ){
Return STRs. Contains (RFC. Get (0). topactivity. getpackagename ());
} Else {
Return false;
}
}

 

Address: http://www.linuxidc.com/Linux/2012-02/54987.htm

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.