Android determines whether the current interface is a desktop (home page)

Source: Internet
Author: User

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!


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.