• Android App 实现分享功能及将应用加入分享列表 (分享功能可自定义需要分享的APP)


    1,自己封装的一个简单的Class:

    public class ShareSample {
    
        /**
         * 获得分享列表(简单实现,只是把核心的东西写了写。不是太实用)
         * **/
        public void getShareList(final String title,final String content,final Activity mActivity)
        {
            final Map<String,ResolveInfo> appInfo =new HashMap<String, ResolveInfo>();
            List<ResolveInfo> appList=getShareTargets(mActivity);
            final String[] items;
            if(appList.size()>0){
                for (int i = 0; i < appList.size(); i++) {
                    ResolveInfo tmp_ri=(ResolveInfo)appList.get(i);
                    //ActivityInfo tmp_ai=tmp_ri.activityInfo;
                    ApplicationInfo apinfo=tmp_ri.activityInfo.applicationInfo;
                    String tmp_appName = apinfo.loadLabel(mActivity.getPackageManager()).toString();
                    //这里就做了一个简单的得到应用名称判断, 实际情况根据 ResolveInfo 这东东 可以得到很多很多有用的东东哦。
                    if(!tmp_appName.equals("微博"))
                    {
                        continue;
                    }
                    appInfo.put(tmp_appName,tmp_ri);
                }
                items=new String[appInfo.size()];
                int j=0;
                for (Map.Entry<String,ResolveInfo> myEntry : appInfo.entrySet()) { 
                    items[j]=myEntry.getKey();
                    j++;
                 } 
                
            }
            else{
                Toast.makeText(mActivity.getApplicationContext(),"暂无分享应用", Toast.LENGTH_SHORT).show();
                return;
            }
            if(appInfo.size()<1)
            {
                Toast.makeText(mActivity.getApplicationContext(),"暂无分享应用", Toast.LENGTH_SHORT).show();
                return;
            }
            /**
             * 这里用对话框简单的一个实现,实际开发中应该要单独抽取出来封装成一个自定义的列表。
             * 
             * **/
            new AlertDialog.Builder(mActivity).setTitle("选择分享") 
            .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int item) { 
                
                CreateShare(title,content,mActivity,appInfo.get(items[item]));
                //CreateShare(title,content,mActivity);
                dialog.cancel(); 
            } 
    
            }).show();//显示对话框
            
        }
        
        /**
         * 实现自定义分享功能(主要就是启动对应的App)
         * **/
        private void CreateShare(String title,String content ,Activity activity,ResolveInfo appInfo) {  
            
            try
            {
                Intent shareIntent=new Intent(Intent.ACTION_SEND);
                 shareIntent.setComponent(new ComponentName(appInfo.activityInfo.packageName, appInfo.activityInfo.name));  
                 //这里就是组织内容了,  
                // shareIntent.setType("text/plain"); 
                 shareIntent.setType("image/*"); 
                 shareIntent.putExtra(Intent.EXTRA_TEXT, content);  
                 shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
                 activity.startActivity(shareIntent);  
                
            }catch (Exception e) {
                e.printStackTrace();
                // TODO: handle exception
            }
            
            
           
        }  
        /**
         * 默认分享列表(网上一大堆)
         * **/
        private void CreateShare(String title,String content ,Activity activity) {  
            Intent intent=new Intent(Intent.ACTION_SEND);
            intent.setType("image/*");   
            intent.putExtra(Intent.EXTRA_SUBJECT, title);   
            intent.putExtra(Intent.EXTRA_TEXT, content);    
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
            activity.startActivity(Intent.createChooser(intent, title));
            
        }
        /**
         * 获得所有带Intent.ACTION_SEND的应用列表。 ResolveInfo 这个东东真不错。
         * **/
        private List<ResolveInfo> getShareTargets(Activity activity){
            Intent intent=new Intent(Intent.ACTION_SEND,null);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            intent.setType("text/plain");
            PackageManager pm=activity.getPackageManager();
            return pm.queryIntentActivities(intent,PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
            }
    }

    2.测试调用:

     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            try
            {
              ShareSample sample =new ShareSample();
              sample.getShareList("分享标题", "要分享的内容哦。呵呵", this);
            }catch (Exception e) {
                e.printStackTrace();
                // TODO: handle exception
            }
    }

    3.应用加入系统分享列表(这个在网上Copy的木有验证过)
    只需在AndroidManifest.xml中加入以下代码:

    <activity android:name=".SharePage" android:label="分享到微博">
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="image/*" />
        </intent-filter>
    </activity>

    4.小结:  

    不是太复杂的东西。但如果不知道 ResolveInfo 这个东东怎么用就不太好搞了。(偶开始就不知道所以纠结了一下下。Android 小菜菜)

    新浪微博 http://www.weibo.com/yeqw 腾讯微博 http://t.qq.com/xingji_yxx 热烈欢迎大家一起交流讨论并提出宝贵意见,多多指导。
  • 相关阅读:
    [leetcode]Merge Intervals
    ffmpeg错误隐藏框架分析
    [置顶] Hash查找,散列查找
    VS2008LINK : fatal error LNK1000: Internal error during IncrBuildImage
    HDU1257:最少拦截系统(LIS)
    ubuntu系统使用dnw下载程序
    ubuntu系统使用minicom终端操作说明
    uboot显示logo的方式
    在XC2440的uboot中挂载U盘,利用FAT文件系统读写U盘文件
    不知道黑片,千万别说你懂“U盘”-解读Nand Flash
  • 原文地址:https://www.cnblogs.com/yeqw1985/p/2909366.html
Copyright © 2020-2023  润新知