Android obtain sharing application list detailed explanation and examples
If the ACTION_SEND attribute is included in the application, it proves that the application can be used for calls and sharing by third-party applications. How to get the sharing list of the function attribute, which is very useful for us to make applications. Recently, when we are doing this function, I have also made a custom sharing list and popped up in PopupWindow.
1. Layout:
popup_share.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="fill_parent" android:layout_height="wrap_content" > <ListView android: android:background="#2F4F4F" android:fadingEdge="none" android:layout_width="fill_parent" android:layout_height="wrap_content" android:cacheColorHint="#00000000" android:divider="#E2DD75" android:dividerHeight="1.0dip" android:headerDividersEnabled="true" android:footerDividersEnabled="false" /> </LinearLayout>
popup_share_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="2.0dip" > <ImageView android: android:layout_width="32.0dip" android:layout_height="32.0dip" android:layout_marginLeft="3.0dip" android:scaleType="fitXY" /> <TextView android: android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="share" android:textColor="@color/white" android:singleLine="true" android:textSize="@dimen/s_size" android:layout_marginLeft="3.0dip" android:layout_marginRight="3.0dip" /> </LinearLayout>
2. Query the list of all supported and shared applications on the mobile phone
public List<ResolveInfo> getShareApps(Context context) { List<ResolveInfo> mApps = new ArrayList<ResolveInfo>(); Intent intent = new Intent(Intent.ACTION_SEND, null); (Intent.CATEGORY_DEFAULT); ("text/plain"); // ("*/*"); PackageManager pManager = (); mApps = (intent, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT); return mApps; }
Note: ApplicationInfo is information obtained from a specific application. This information is collected from the corresponding <application> tags.
The ResolveInfo class is obtained by parsing an intent corresponding to an IntentFilter. It corresponds in part to the information collected from the <intent> tag.
Get the List list, I built my own AppInfo class, just build one by myself
private List<AppInfo> getShareAppList() { List<AppInfo> shareAppInfos = new ArrayList<AppInfo>(); PackageManager packageManager = getPackageManager(); List<ResolveInfo> resolveInfos = getShareApps(mContext); if (null == resolveInfos) { return null; } else { for (ResolveInfo resolveInfo : resolveInfos) { AppInfo appInfo = new AppInfo(); (); // showLog_I(TAG, "pkg>" + + ";name>" + ); (); ((packageManager).toString()); ((packageManager)); (appInfo); } } return shareAppInfos; }
3. PopupWindow implementation
private void initSharePopupWindow(View parent) { PopupWindow sharePopupWindow = null; View view = null; ListView shareList = null; if(null == sharePopupWindow) { //Load the layout file view = ().inflate(.popup_share, null); shareList = (ListView) (.share_list); List<AppInfo> shareAppInfos = getShareAppList(); final ShareCustomAdapter adapter = new ShareCustomAdapter(mContext, shareAppInfos); (adapter); (new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub Intent shareIntent = new Intent(Intent.ACTION_SEND); AppInfo appInfo = (AppInfo) (position); (new ComponentName((), ())); ("text/plain"); // ("*/*"); //This is the organization content. (Intent.EXTRA_TEXT, "test"); (Intent.FLAG_ACTIVITY_NEW_TASK); (shareIntent); } }); sharePopupWindow = new PopupWindow(view, (int)(160 * density), .WRAP_CONTENT); } // Make it focus (true); //Setting allows clicks to disappear outside (true); // This is to click "Back Back" to make it disappear and will not affect your background (new BitmapDrawable()); //xoff,yoff is offset based on the lower left corner of anchor. Positive numbers represent the right below, negative numbers represent (top left above) //showAsDropDown(parent, xPos, yPos); (parent, -5, 5); }
Note: Just build one of ShareCustomAdapter by yourself. (Showing there will be an icon and a shared name)
Thank you for reading, I hope it can help you. Thank you for your support for this site!