Android 自定义分享列表ACTION_SEND

来源:互联网 发布:豫广网络投诉电话 编辑:程序博客网 时间:2024/05/18 22:43

看到最近都在做自定义的东西,因为比较灵活,还可以摆脱系统自身不怎么漂亮的UI,(大家都懂得)所以自己也做了下自定义的分享列表,用PopupWindow的方式弹出。
先上效果图:

1、布局:
popup_share.xml
[html] view plaincopy
  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.   <ListView   
  6.         android:id="@+id/share_list"   
  7.         android:background="#2F4F4F"   
  8.         android:fadingEdge="none"   
  9.         android:layout_width="fill_parent"   
  10.         android:layout_height="wrap_content"   
  11.         android:cacheColorHint="#00000000"   
  12.         android:divider="#E2DD75"   
  13.         android:dividerHeight="1.0dip"   
  14.         android:headerDividersEnabled="true"   
  15.         android:footerDividersEnabled="false" />  
  16. </LinearLayout>  
popup_share_item.xml
[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:gravity="center_vertical"   
  4.     android:layout_width="wrap_content"   
  5.     android:layout_height="wrap_content"   
  6.     android:padding="2.0dip" >  
  7.     <ImageView   
  8.         android:id="@+id/share_item_icon"   
  9.         android:layout_width="32.0dip"   
  10.         android:layout_height="32.0dip"   
  11.         android:layout_marginLeft="3.0dip"   
  12.         android:scaleType="fitXY" />  
  13.     <TextView   
  14.         android:id="@+id/share_item_name"   
  15.         android:gravity="center"   
  16.         android:layout_width="wrap_content"   
  17.         android:layout_height="wrap_content"   
  18.         android:text="分享"   
  19.         android:textColor="@color/white"   
  20.         android:singleLine="true"   
  21.         android:textSize="@dimen/s_size"   
  22.         android:layout_marginLeft="3.0dip"   
  23.         android:layout_marginRight="3.0dip" />  
  24. </LinearLayout>  

2、查询手机内所有支持分享的应用列表
[java] view plaincopy
  1. public List<ResolveInfo> getShareApps(Context context) {    
  2.         List<ResolveInfo> mApps = new ArrayList<ResolveInfo>();  
  3.         Intent intent = new Intent(Intent.ACTION_SEND, null);  
  4.         intent.addCategory(Intent.CATEGORY_DEFAULT);  
  5.         intent.setType("text/plain");  
  6. //      intent.setType("*/*");  
  7.         PackageManager pManager = context.getPackageManager();  
  8.         mApps = pManager.queryIntentActivities(intent,   
  9.                 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);  
  10.         return mApps;  
  11.     }  
注:ApplicationInfo是从一个特定的应用得到的信息。这些信息是从相对应的Androdimanifest.xml的< application>标签中收集到的。
ResolveInfo这个类是通过解析一个与IntentFilter相对应的intent得到的信息。它部分地对应于从AndroidManifest.xml的< intent>标签收集到的信息。
得到List列表,我自建的AppInfo类,自己建一个就行
[java] view plaincopy
  1. private List<AppInfo> getShareAppList() {   
  2.         List<AppInfo> shareAppInfos = new ArrayList<AppInfo>();  
  3.         PackageManager packageManager = getPackageManager();  
  4.         List<ResolveInfo> resolveInfos = getShareApps(mContext);  
  5.         if (null == resolveInfos) {  
  6.             return null;  
  7.         } else {  
  8.             for (ResolveInfo resolveInfo : resolveInfos) {  
  9.                 AppInfo appInfo = new AppInfo();  
  10.                 appInfo.setAppPkgName(resolveInfo.activityInfo.packageName);  
  11. //              showLog_I(TAG, "pkg>" + resolveInfo.activityInfo.packageName + ";name>" + resolveInfo.activityInfo.name);  
  12.                 appInfo.setAppLauncherClassName(resolveInfo.activityInfo.name);  
  13.                 appInfo.setAppName(resolveInfo.loadLabel(packageManager).toString());  
  14.                 appInfo.setAppIcon(resolveInfo.loadIcon(packageManager));  
  15.                 shareAppInfos.add(appInfo);  
  16.             }  
  17.         }         
  18.         return shareAppInfos;  
  19.     }  
3、弹出PopupWindow的实现
[java] view plaincopy
  1. private void initSharePopupWindow(View parent) {  
  2.         PopupWindow sharePopupWindow = null;  
  3.         View view = null;  
  4.         ListView shareList = null;  
  5.         if(null == sharePopupWindow) {  
  6.             //加载布局文件  
  7.             view = LayoutInflater.from(DetailExchangeActivity.this).inflate(R.layout.popup_share, null);  
  8.             shareList = (ListView) view.findViewById(R.id.share_list);  
  9.             List<AppInfo> shareAppInfos = getShareAppList();  
  10.             final ShareCustomAdapter adapter = new ShareCustomAdapter(mContext, shareAppInfos);  
  11.             shareList.setAdapter(adapter);  
  12.               
  13.             shareList.setOnItemClickListener(new OnItemClickListener() {  
  14.   
  15.                 @Override  
  16.                 public void onItemClick(AdapterView<?> parent, View view,  
  17.                         int position, long id) {  
  18.                     // TODO Auto-generated method stub  
  19.                     Intent shareIntent = new Intent(Intent.ACTION_SEND);  
  20.                     AppInfo appInfo = (AppInfo) adapter.getItem(position);  
  21.                     shareIntent.setComponent(new ComponentName(appInfo.getAppPkgName(), appInfo.getAppLauncherClassName()));  
  22.                     shareIntent.setType("text/plain");  
  23. //                  shareIntent.setType("*/*");  
  24.                     //这里就是组织内容了,  
  25.                     shareIntent.putExtra(Intent.EXTRA_TEXT, "测试,这里发送推广地址");  
  26.                     shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  27.                     DetailExchangeActivity.this.startActivity(shareIntent);  
  28.                 }  
  29.             });  
  30.               
  31.             sharePopupWindow = new PopupWindow(view,   
  32.                     (int)(160 * density), LinearLayout.LayoutParams.WRAP_CONTENT);  
  33.         }  
  34.         //使其聚焦  
  35.         sharePopupWindow.setFocusable(true);  
  36.         //设置允许在外点击消失  
  37.         sharePopupWindow.setOutsideTouchable(true);  
  38.         // 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景  
  39.         sharePopupWindow.setBackgroundDrawable(new BitmapDrawable());  
  40.         //xoff,yoff基于anchor的左下角进行偏移。正数表示下方右边,负数表示(上方左边)  
  41.         //showAsDropDown(parent, xPos, yPos);  
  42.         sharePopupWindow.showAsDropDown(parent, -55);  
  43.     }  
注:ShareCustomAdapter自己建一个就行了。(有一个图标和一个分享的名)