Android and android Official Website
Sometimes you want to know whether an APP has registered a clear intent. For example, you want to check whether a receiver exists, then, enable some functions in your AP based on whether the hacker exists. We can check PackageManager.
Code
public boolean isIntentAvailable(Context context, String action) { final PackageManager packageManager = context.getPackageManager(); final Intent intent = new Intent(action); List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); if (resolveInfo.size() > 0) { return true; } return false; }
Dry Goods
Generally, the intent-filter must be set for the launcher activity in the manifest of the APP:
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
You can use queryIntentActivities to obtain the launcher:
private void loadApps() { Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); mApps = getPackageManager().queryIntentActivities(mainIntent, 0); }
We can use a gridview in our app to show which apps are on the current mobile phone:
import java.util.List; import android.app.Activity; import android.content.Intent; import android.content.pm.ResolveInfo; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; public class MyHome extends Activity { GridView mGrid; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); loadApps(); setContentView(R.layout.main); mGrid = (GridView) findViewById(R.id.apps_list); mGrid.setAdapter(new AppsAdapter()); } private List<ResolveInfo> mApps; private void loadApps() { Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); mApps = getPackageManager().queryIntentActivities(mainIntent, 0); } public class AppsAdapter extends BaseAdapter { public AppsAdapter() { } public View getView(int position, View convertView, ViewGroup parent) { ImageView i; if (convertView == null) { i = new ImageView(MyHome.this); i.setScaleType(ImageView.ScaleType.FIT_CENTER); i.setLayoutParams(new GridView.LayoutParams(50, 50)); } else { i = (ImageView) convertView; } ResolveInfo info = mApps.get(position); i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager())); return i; } public final int getCount() { return mApps.size(); } public final Object getItem(int position) { return mApps.get(position); } public final long getItemId(int position) { return position; } } }
You can click the listener to enable the app:
@Override
Public void onItemClick (AdapterView <?> Parent, View view, int position, long id) {ResolveInfo info = mApps. get (position); // The package name of the application String pkg = info. activityInfo. packageName; // main activity class of the application String cls = info. activityInfo. name; ComponentName componet = new ComponentName (pkg, cls); Intent I = new Intent (); I. setComponent (componet); startActivity (I );}
I am the dividing line of tiantiao