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

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

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动态加载Activity原理详解

这篇文章主要介绍了Android动态加载Activity原理详解的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android App中实现相册瀑布流展示的实例分享

这篇文章主要介绍了Android App中实现相册瀑布流展示的实例分享,例子中利用到了缓存LruCache类的相关算法来解决大量加载问题,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发中ImageLoder进行图片加载和缓存

这篇文章主要介绍了Android开发中ImageLoder进行图片加载和缓存的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Android Activity之间切换传递数据的方法

这篇文章主要介绍了详解Android Activity之间切换传递数据的方法 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Android 手机卫士设置向导页面

这篇文章主要介绍了详解Android 手机卫士设置向导页面的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

深入浅析Android手机卫士保存密码时进行md5加密

一般的手机没有root权限,进不去data/data目录,当手机刷机了后,拥有root权限,就可以进入data/data目录,查看我们保存的密码文件,因此我们需要对存入的密码进行MD5加密,接下来通过本文给大家介绍Android手机卫士保存密码时进行md5加密,需要的朋友一起学习
收藏 0 赞 0 分享

Android仿百度外卖自定义下拉刷新效果

大家在使用百度外卖的订餐的时候,会看到有个下拉刷新功能非常不错,今天小编就通过代码给大家介绍android仿百度外卖自定义下拉刷新,感兴趣的朋友一起学习吧
收藏 0 赞 0 分享

详解Android中Service服务的基础知识及编写方法

这篇文章主要介绍了详解Android中Service服务的基础知识及编写方法,包括Service的启动流程及生命周期等基本内容,需要的朋友可以参考下
收藏 0 赞 0 分享

实例讲解Android中的AIDL内部进程通信接口使用

这篇文章主要通过实例介绍了Android中的AIDL内部进程通信接口使用,文中通过一个音乐播放的服务编写例子来讲解AIDL的传递对象及一般使用步骤,需要的朋友可以参考下
收藏 0 赞 0 分享

浅析Android系统的架构以及程序项目的目录结构

这篇文章主要介绍了Android系统的架构以及程序项目的目录结构,是安卓开发入门学习中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多