Android编程实现自定义分享列表ACTION_SEND功能的方法

所属分类: 软件编程 / Android 阅读数: 32
收藏 0 赞 0 分享

本文实例讲述了Android编程实现自定义分享列表ACTION_SEND功能的方法。分享给大家供大家参考,具体如下:

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

先上效果图:

1、布局:

popup_share.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_share_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/share_item_icon"
    android:layout_width="32.0dip"
    android:layout_height="32.0dip"
    android:layout_marginLeft="3.0dip"
    android:scaleType="fitXY" />
  <TextView
    android:id="@+id/share_item_name"
    android:gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="分享"
    android:textColor="@color/white"
    android:singleLine="true"
    android:textSize="@dimen/s_size"
    android:layout_marginLeft="3.0dip"
    android:layout_marginRight="3.0dip" />
</LinearLayout>

2、查询手机内所有支持分享的应用列表

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;
}

注:ApplicationInfo是从一个特定的应用得到的信息。这些信息是从相对应的Androdimanifest.xml的< application>标签中收集到的。

ResolveInfo这个类是通过解析一个与IntentFilter相对应的intent得到的信息。它部分地对应于从AndroidManifest.xml的< intent>标签收集到的信息。

得到List列表,我自建的AppInfo类,自己建一个就行

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、弹出PopupWindow的实现

private void initSharePopupWindow(View parent) {
    PopupWindow sharePopupWindow = null;
    View view = null;
    ListView shareList = null;
    if(null == sharePopupWindow) {
      //加载布局文件
      view = LayoutInflater.from(DetailExchangeActivity.this).inflate(R.layout.popup_share, null);
      shareList = (ListView) view.findViewById(R.id.share_list);
      List<AppInfo> shareAppInfos = getShareAppList();
      final ShareCustomAdapter adapter = new ShareCustomAdapter(mContext, shareAppInfos);
      shareList.setAdapter(adapter);
      shareList.setOnItemClickListener(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) adapter.getItem(position);
          shareIntent.setComponent(new ComponentName(appInfo.getAppPkgName(), appInfo.getAppLauncherClassName()));
          shareIntent.setType("text/plain");
//         shareIntent.setType("*/*");
          //这里就是组织内容了,
          shareIntent.putExtra(Intent.EXTRA_TEXT, "测试,这里发送推广地址");
          shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          DetailExchangeActivity.this.startActivity(shareIntent);
        }
      });
      sharePopupWindow = new PopupWindow(view,
          (int)(160 * density), LinearLayout.LayoutParams.WRAP_CONTENT);
    }
    //使其聚焦
    sharePopupWindow.setFocusable(true);
    //设置允许在外点击消失
    sharePopupWindow.setOutsideTouchable(true);
    // 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景
    sharePopupWindow.setBackgroundDrawable(new BitmapDrawable());
    //xoff,yoff基于anchor的左下角进行偏移。正数表示下方右边,负数表示(上方左边)
    //showAsDropDown(parent, xPos, yPos);
    sharePopupWindow.showAsDropDown(parent, -5, 5);
}

注:ShareCustomAdapter自己建一个就行了。(有一个图标和一个分享的名)

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

更多精彩内容其他人还在看

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中的Bitmap序列化失败的解决方法

这篇文章主要介绍了Android中的Bitmap序列化失败的解决方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android自定义通用标题栏CustomTitleBar

这篇文章主要为大家详细介绍了Android自定义通用标题栏CustomTitleBar,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android组合控件自定义标题栏

这篇文章主要为大家详细介绍了Android组合控件自定义标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义复合控件实现通用标题栏

这篇文章主要为大家详细介绍了Android自定义复合控件实现通用标题栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ExpandableListView实现简单二级列表

这篇文章主要为大家详细介绍了ExpandableListView实现简单二级列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多