Android custom share list action_send

Source: Internet
Author: User
We can see that we have recently made custom things. Because of its flexibility, we can also get rid of the system's own pretty UI (everyone knows) so I also made a custom share list, which is displayed in the popupwindow mode. First:
1. Layout:Popup_pai.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="wrap_content" >  <ListView android:id="@+id/share_list" 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_assist_item.xml

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: gravity = "center_vertical" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: padding = "2.0dip"> <imageview Android: Id = "@ + ID/pai_item_icon" Android: layout_width = "32.0dip" Android: layout_height = "32.0dip" Android: layout_marginleft = "3.0dip" Android: scaletype = "fitxy"/> <textview Android: Id = "@ + ID/pai_item_name" Android: gravity = "center" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "sharing" 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 apps that support sharing 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.addCategory(Intent.CATEGORY_DEFAULT);intent.setType("text/plain");//intent.setType("*/*");PackageManager pManager = context.getPackageManager();mApps = pManager.queryIntentActivities(intent, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);return mApps;}

Note: applicationinfo is the information obtained from a specific application. This information is collected from the <Application> tag of androdimanifest. xml. The resolveinfo class is obtained by parsing the intent corresponding to the intentfilter. It partially corresponds to the information collected from the <intent> tag of androidmanifest. xml. Obtain the list. Create one of my self-built appinfo classes.

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();appInfo.setAppPkgName(resolveInfo.activityInfo.packageName);//showLog_I(TAG, "pkg>" + resolveInfo.activityInfo.packageName + ";name>" + resolveInfo.activityInfo.name);appInfo.setAppLauncherClassName(resolveInfo.activityInfo.name);appInfo.setAppName(resolveInfo.loadLabel(packageManager).toString());appInfo.setAppIcon(resolveInfo.loadIcon(packageManager));shareAppInfos.add(appInfo);}}return shareAppInfos;}

3. Pop-up popupwindow implementation

Private void init1_popupwindow (view parent) {popupwindow export popupwindow = NULL; view = NULL; listview sharelist = NULL; If (null = export popupwindow) {// load layout file view = layoutinflater. from (detailexchangeactivity. this ). inflate (R. layout. popup_share, null); sharelist = (listview) view. findviewbyid (R. id. pai_list); List <appinfo> export appinfos = get1_applist (); Final sharecustomadapter adapter = new Sharecustomadapter (mcontext, shareappinfos); sharelist. setadapter (adapter); sharelist. setonitemclicklistener (New onitemclicklistener () {@ overridepublic void onitemclick (adapterview <?> Parent, view, int position, long ID) {// todo auto-generated method stubintent specify intent = new intent (intent. action_send); appinfo = (appinfo) adapter. getitem (position); specified intent. setcomponent (New componentname (appinfo. getapppkgname (), appinfo. getapplauncherclassname (); Specify intent. settype ("text/plain"); // specify intent. settype ("*/*"); // here is the organization content, which contains intent. putextra (intent. extra_text, "Test, send the promotion address here"); your intent. setflags (intent. flag_activity_new_task); detailexchangeactivity. this. startactivity (intent) ;}}); intent popupwindow = new popupwindow (view, (INT) (160 * density), linearlayout. layoutparams. wrap_content);} // enables it to focus on sharepopupwindow. setfocusable (true); // you can specify whether or not to click sharepopupwindow. setoutsidetouchable (true); // you can click "back" to make it disappear without affecting your background. setbackgrounddrawab Le (New bitmapdrawable (); // xoff, yoff offset based on the lower left corner of anchor. Positive number indicates the right of the lower part, negative number indicates (left above) // showasdropdown (parent, xpos, ypos); then popupwindow. showasdropdown (parent,-5, 5 );}

Note: You can create one sharecustomadapter on your own. (There is an icon and a name for sharing)


Reference address: Packageinfo, resolveinfo

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.