Android获取分享应用列表详解及实例

发布时间 - 2026-01-11 00:46:08    点击率:

Android获取分享应用列表详解及实例

如果在应用的AndroidManifest.xml中含有 ACTION_SEND 属性,那就证明该应用可以供第三方应用进行调用分享,那怎么获取函数该属性的分享列表了,这对我们做应用的非常有用;最近在做该功能,自己也做了下自定义的分享列表,用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, "test"); 
          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自定义PopupWindow仿点击弹出分享功能  # 关于Android实现简单的微信朋友圈分享功能  # Android编程实现调用系统分享功能示例  # 简述Android中实现APP文本内容的分享发送与接收方法  # 是从  # 弹出  # 使其  # 相对应  # 会有  # 那就  # 也能  # 就行  # 希望能  # 这对  # 自定义  # 谢谢大家  # 第三方  # 也做  # 以供  # 应于  # 是为了  # 加载  # 就行了  # 中含有 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: 韩国服务器如何优化跨境访问实现高效连接?  Python进程池调度策略_任务分发说明【指导】  简单实现Android文件上传  Zeus浏览器网页版官网入口 宙斯浏览器官网在线通道  JavaScript常见的五种数组去重的方式  Python图片处理进阶教程_Pillow滤镜与图像增强  Laravel如何安装Breeze扩展包_Laravel用户注册登录功能快速实现【流程】  网站制作报价单模板图片,小松挖机官方网站报价?  如何用wdcp快速搭建高效网站?  香港服务器租用每月最低只需15元?  javascript中对象的定义、使用以及对象和原型链操作小结  HTML 中动态设置元素 name 属性的正确语法详解  javascript中数组(Array)对象和字符串(String)对象的常用方法总结  Windows Hello人脸识别突然无法使用  Laravel Seeder填充数据教程_Laravel模型工厂Factory使用  HTML5空格和nbsp有啥关系_nbsp的作用及使用场景【说明】  新三国志曹操传主线渭水交兵攻略  如何在服务器上配置二级域名建站?  Laravel如何配置和使用队列处理异步任务_Laravel队列驱动与任务分发实例  微信小程序 闭包写法详细介绍  香港服务器租用费用高吗?如何避免常见误区?  Edge浏览器怎么启用睡眠标签页_节省电脑内存占用优化技巧  什么是JavaScript解构赋值_解构赋值有哪些实用技巧  Laravel如何使用模型观察者?(Observer代码示例)  javascript如何操作浏览器历史记录_怎样实现无刷新导航  香港服务器如何优化才能显著提升网站加载速度?  如何在搬瓦工VPS快速搭建网站?  php485函数参数是什么意思_php485各参数详细说明【介绍】  canvas 画布在主流浏览器中的尺寸限制详细介绍  三星网站视频制作教程下载,三星w23网页如何全屏?  百度输入法ai组件怎么删除 百度输入法ai组件移除工具  Laravel如何处理JSON字段_Eloquent原生JSON字段类型操作教程  北京企业网站设计制作公司,北京铁路集团官方网站?  详解jQuery中的事件  Laravel storage目录权限问题_Laravel文件写入权限设置  家族网站制作贴纸教程视频,用豆子做粘帖画怎么制作?  如何快速重置建站主机并恢复默认配置?  Laravel怎么实现验证码(Captcha)功能  MySQL查询结果复制到新表的方法(更新、插入)  如何用PHP快速搭建高效网站?分步指南  JavaScript Ajax实现异步通信  SQL查询语句优化的实用方法总结  Laravel项目如何进行性能优化_Laravel应用性能分析与优化技巧大全  Win11怎么关闭资讯和兴趣_Windows11任务栏设置隐藏小组件  手机软键盘弹出时影响布局的解决方法  Laravel如何生成URL和重定向?(路由助手函数)  如何在Windows环境下新建FTP站点并设置权限?  北京网站制作费用多少,建立一个公司网站的费用.有哪些部分,分别要多少钱?  网页制作模板网站推荐,网页设计海报之类的素材哪里好?  HTML透明颜色代码怎么让图片透明_给img元素加透明色的技巧【方法】