【Android】分享

               

本讲主要介绍如何在自己的应用中实现分享功能,同时介绍如何将自己的程序加入分享列表。

比如有一张图片,想要分享到校内上...看看效果图吧。

本次我们就是要做这样的效果,同时把自己的应用也加入到分享列表中。

调出"共享方式"的代码如下:

          Intent intent=new Intent(Intent.ACTION_SEND);                    intent.setType("text/plain");          intent.putExtra(Intent.EXTRA_SUBJECT, "分享");          intent.putExtra(Intent.EXTRA_TEXT, "好东西,与您分享!");          startActivity(Intent.createChooser(intent, getTitle())); 

如何让自己的应用也加入这个分享列表呢?

答案其实也很简单,只需在AndroidManifest.xml配置文件中activity标签之间加入以下代码:

      <intent-filter>    <action android:name="android.intent.action.SEND">    </action>    <category android:name="android.intent.category.DEFAULT">    </category>    <data android:mimeType="text/plain">    </data>   </intent-filter>

注意,以上程序最好在真机上测试,模拟器上看不出效果!

另外,有的同学可能不想把这些应用都显示出来,比如指向要短信、邮件,那么可以通过先获取分享列表,然后自己过滤的方式来实现。

获取分享列表代码:

    public List<ResolveInfo> getShareTargets(){     List<ResolveInfo> mApps = new ArrayList<ResolveInfo>();        Intent intent=new Intent(Intent.ACTION_SEND,null);        intent.addCategory(Intent.CATEGORY_DEFAULT);        intent.setType("text/plain");        PackageManager pm=this.getPackageManager();        mApps=pm.queryIntentActivities(intent,PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);              return mApps;    } 

           

猜你喜欢

转载自blog.csdn.net/qq_44912644/article/details/89501252