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