Android and android Official Website

Source: Internet
Author: User

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

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.