Android--Queryintentactivities

Source: Internet
Author: User

At some point you want to know if an app has a specific intent, say you want to check if a receiver exists, and then enable certain functions on your AP based on the presence of the receiver. We can check it by Packagemanager.

Code
 Public Booleanisintentavailable (Context context, String action) {FinalPackagemanager Packagemanager =Context.getpackagemanager (); FinalIntent Intent =NewIntent (action); List<ResolveInfo> ResolveInfo =packagemanager.queryintentactivities (Intent, packagemanager.match_default_only); if(Resolveinfo.size () > 0) {          return true; }     return false; }
Dry

In the general app manifest, the activity to be set to launcher is always set Intent-filter:

< Intent-filter >                  <  android:name= "Android.intent.action.MAIN"/>                  <   android:name= "Android.intent.category.LAUNCHER"/>  </  intent-filter>

There are launcher that can be obtained through Queryintentactivities:

Private void Loadapps () {                   newnull);                   Mainintent.addcategory (intent.category_launcher);                      = Getpackagemanager (). Queryintentactivities (mainintent, 0);           }

We can use a GridView on our app to show you what apps are currently on your phone:

Importjava.util.List; Importandroid.app.Activity; Importandroid.content.Intent; ImportAndroid.content.pm.ResolveInfo; ImportAndroid.os.Bundle; ImportAndroid.view.View; ImportAndroid.view.ViewGroup; ImportAndroid.widget.BaseAdapter; ImportAndroid.widget.GridView; ImportAndroid.widget.ImageView;  Public classMyHomeextendsActivity {GridView mgrid; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                   Loadapps ();                   Setcontentview (R.layout.main); Mgrid=(GridView) Findviewbyid (r.id.apps_list); Mgrid.setadapter (NewAppsadapter ()); }                       PrivateList<resolveinfo>Mapps; Private voidLoadapps () {Intent mainintent=NewIntent (Intent.action_main,NULL);                      Mainintent.addcategory (Intent.category_launcher); Mapps= Getpackagemanager (). Queryintentactivities (mainintent, 0); }                Public classAppsadapterextendsBaseadapter { PublicAppsadapter () {} PublicView GetView (intposition, View Convertview, ViewGroup parent)                               {ImageView i; if(Convertview = =NULL) {i=NewImageView (myhome. This);                                   I.setscaletype (ImageView.ScaleType.FIT_CENTER); I.setlayoutparams (NewGridview.layoutparams (50, 50)); } Else{i=(ImageView) Convertview; } ResolveInfo Info=Mapps.get (position);                               I.setimagedrawable (Info.activityInfo.loadIcon (Getpackagemanager ())); returni; }                            Public Final intGetCount () {returnmapps.size (); }                        Public FinalObject GetItem (intposition) {                           returnMapps.get (position); }                        Public Final LongGetitemid (intposition) {                           returnposition; }           }   }

You can set up a click Listener to open the app:

 Public voidOnitemclick (adapterview<?> Parent, view view,intPositionLongID) {ResolveInfo info=Mapps.get (position); //the package name of the appString pkg =Info.activityInfo.packageName; //The main activity class of the applicationString CLS =Info.activityInfo.name; ComponentName componet=Newcomponentname (pkg, CLS); Intent I=NewIntent ();                  I.setcomponent (componet);          StartActivity (i); }
I'm the dividing line of the king of the Land Tiger.

Android--Queryintentactivities

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.