android过滤第三应用列表

转载自:http://blog.csdn.net/santicom/article/details/7465751

调用所有匹配的第三应用:

[java]  view plain copy
  1. public class AppFilterActivity extends Activity  
  2. {  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState)  
  5.     {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.main);  
  8.   
  9.         Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);  
  10.         intent.setType("image/png");  
  11.   
  12.         Intent chooserIntent = Intent.createChooser(intent, "Select app");  
  13.         if (chooserIntent == null)  
  14.         {  
  15.             return;  
  16.         }  
  17.         startActivity(chooserIntent);  
  18.     }  
  19. }  

 


过滤app列表中的某个app:

1,

[java]  view plain copy
  1. public class AppFilterActivity extends Activity  
  2. {  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState)  
  5.     {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.main);  
  8.   
  9.         Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);  
  10.         intent.setType("image/png");  
  11.   
  12.         showSetAsList(intent);  
  13.     }  
  14.   
  15.     private void showSetAsList(Intent intent)  
  16.     {  
  17.         PackageManager mPackageManager = AppFilterActivity.this.getPackageManager();  
  18.         List mShareAppLst = mPackageManager.queryIntentActivities(intent, 0);  
  19.         List mAppLst = new ArrayList();  
  20.         int numActivities = mShareAppLst.size();  
  21.         // 遍历设置为列表,移除设置为列表中的联系人  
  22.         for (int i = 0; i < numActivities; i++)  
  23.         {  
  24.             ResolveInfo reinfo = mShareAppLst.get(i);  
  25.             if (reinfo.activityInfo.name.equals("com.android.contacts.activities.AttachPhotoActivity"))  
  26.             {  
  27.                 continue;  
  28.             }  
  29.             mAppLst.add(reinfo);  
  30.         }  
  31.         ChooseAppAdapter mAdapter = new ChooseAppAdapter(mAppLst, AppFilterActivity.this.getApplicationContext());  
  32.         AlertDialog.Builder alertDialog = new AlertDialog.Builder(AppFilterActivity.this);  
  33.         alertDialog.setTitle("select app");  
  34.         alertDialog.setAdapter(mAdapter, new ShareAppItemClickListener(intent, mAppLst, mAdapter)).create().show();  
  35.   
  36.     }  
  37.   
  38.     public class ChooseAppAdapter extends BaseAdapter  
  39.     {  
  40.         private List mDialogList = null;  
  41.         private PackageManager mPackageManager = null;  
  42.         private LayoutInflater mInflater = null;  
  43.   
  44.         public ChooseAppAdapter(List mDialogList, Context context)  
  45.         {  
  46.             this.mDialogList = mDialogList;  
  47.             this.mPackageManager = context.getPackageManager();  
  48.             sort();  
  49.             mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  50.         }  
  51.   
  52.         @Override  
  53.         public int getCount()  
  54.         {  
  55.             return mDialogList != null ? mDialogList.size() : 0;  
  56.         }  
  57.   
  58.         @Override  
  59.         public Object getItem(int position)  
  60.         {  
  61.             return position;  
  62.         }  
  63.   
  64.         @Override  
  65.         public long getItemId(int position)  
  66.         {  
  67.             return position;  
  68.         }  
  69.   
  70.         @Override  
  71.         public View getView(int position, View convertView, ViewGroup parent)  
  72.         {  
  73.             if (position >= mDialogList.size())  
  74.             {  
  75.                 return null;  
  76.             }  
  77.             View view;  
  78.             if (convertView == null)  
  79.             {  
  80.                 view = mInflater.inflate(R.layout.share_menu_item, parent, false);  
  81.             } else  
  82.             {  
  83.                 view = convertView;  
  84.             }  
  85.             ImageView categoryIcon = (ImageView) view.findViewById(R.id.menu_icon);  
  86.             categoryIcon.setImageDrawable(((ResolveInfo) mDialogList.get(position)).loadIcon(mPackageManager));  
  87.             TextView categoryName = (TextView) view.findViewById(R.id.menu_title);  
  88.             categoryName.setText(((ResolveInfo) mDialogList.get(position)).loadLabel(mPackageManager));  
  89.             return view;  
  90.         }  
  91.   
  92.         public void releaseData()  
  93.         {  
  94.             mDialogList.clear();  
  95.             mPackageManager = null;  
  96.             mInflater = null;  
  97.         }  
  98.   
  99.         public void sort()  
  100.         {  
  101.             if (getCount() > 1)  
  102.             {  
  103.                 ResolveInfo.DisplayNameComparator rComparator = new ResolveInfo.DisplayNameComparator(mPackageManager);  
  104.                 Collections.sort(mDialogList, rComparator);  
  105.             }  
  106.         }  
  107.     }  
  108.   
  109.     public class ShareAppItemClickListener implements DialogInterface.OnClickListener  
  110.     {  
  111.         Intent intent = null;  
  112.         List mShareAppLst = null;  
  113.         ChooseAppAdapter mAdapter = null;  
  114.   
  115.         public ShareAppItemClickListener(Intent intent, List mShareAppLst, ChooseAppAdapter mAdapter)  
  116.         {  
  117.             this.intent = intent;  
  118.             this.mShareAppLst = mShareAppLst;  
  119.             this.mAdapter = mAdapter;  
  120.         }  
  121.   
  122.         @Override  
  123.         public void onClick(DialogInterface dialog, int which)  
  124.         {  
  125.             Intent resolvedIntent = new Intent(intent);  
  126.             ResolveInfo info = mShareAppLst.get(which);  
  127.             ActivityInfo ai = info.activityInfo;  
  128.             resolvedIntent.setComponent(new ComponentName(ai.applicationInfo.packageName, ai.name));  
  129.   
  130.             try  
  131.             {  
  132.                 startActivity(resolvedIntent);  
  133.             } catch (Exception ex)  
  134.             {  
  135.                 ex.printStackTrace();  
  136.             }  
  137.             releaseData();  
  138.         }  
  139.   
  140.         public void releaseData()  
  141.         {  
  142.             intent = null;  
  143.             mShareAppLst.clear();  
  144.             mAdapter.releaseData();  
  145.         }  
  146.     }  
  147. }  


2, share_menu_item.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:gravity="center_vertical"  
  6.     android:orientation="horizontal" >  
  7.   
  8.     <ImageView  
  9.         android:id="@+id/menu_icon"  
  10.         android:layout_width="48dip"  
  11.         android:layout_height="48dip"  
  12.         android:layout_marginBottom="2dip"  
  13.         android:layout_marginLeft="5dip"  
  14.         android:layout_marginTop="2dip" />  
  15.   
  16.     <TextView  
  17.         android:id="@+id/menu_title"  
  18.         android:layout_width="match_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:ellipsize="marquee"  
  21.         android:gravity="center_vertical"  
  22.         android:paddingLeft="10dip"  
  23.         android:textAppearance="?android:attr/textAppearanceMedium" />  
  24.   
  25. LinearLayout>