android分享功能

来源:互联网 发布:剑灵刘亦菲捏脸数据图 编辑:程序博客网 时间:2024/05/22 13:06

在Android系统中如何给应用增加分享功能,怎样将应用加入系统的分享选择列表?

此文由于时间问题,表述不准确,所以我做了更新,需要的朋友可以去这个地址查看:http://blog.csdn.net/brokge/article/details/49177555

在Android系统中如何给应用增加分享功能

Intent.createChooser()方法用来弹出系统分享列表。

createChooser方法接受Intent做参数,也同时接纳了Intent里面要求的filter(ACTION_SEND),只有支持ACTION_SEND的Activity才会被列入可选列表

查看Intent对应的组件是否存在,可查看Android判断Intent是否存在,是否可用

<span style="font-size:18px;">public static void shareText(Context context, String title, String text) {    Intent intent = new Intent(Intent.ACTION_SEND);    intent.setType("text/plain");    intent.putExtra(Intent.EXTRA_SUBJECT, title);    intent.putExtra(Intent.EXTRA_TEXT, text);    context.startActivity(Intent.createChooser(intent, title));}</span>

通过上面的方法就可以实现分享功能了,用户可以根据自己的需求随意添加点击来触发这个事件!

怎样将自己的应用加入系统的分享选择列表?

先看一个腾讯微博的例子(网友反编译后的例子)
<activity android:name=".activity.MicroBlogInput" android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation" android:windowSoftInputMode="stateAlwaysVisible|adjustResize"><intent-filter android:label="@string/albums_sendbyWBlog">                <action android:name="android.intent.action.SEND" />                <data android:mimeType="image/*" />                         <category android:name="android.intent.category.DEFAULT" />           </intent-filter></activity> 


通过上面的可以看出下面这些是关键:
<intent-filter android:label="@string/albums_sendbyWBlog">               <action android:name="android.intent.action.SEND" />                <data android:mimeType="image/*" />                         <category android:name="android.intent.category.DEFAULT" />            </intent-filter>


那我们就在我们的程序中添加相应的代码即可以实现
<data android:mimeType="image/*" />   //可以是text/plain

如果自定义弹出列表的项(毕竟如果程序安装的多的话,会出现好长好长的列表)


/* 获得支持ACTION_SEND的应用列表 */private List<ResolveInfo> getShareTargets(){Intent intent=new Intent(Intent.ACTION_SEND,null);intent.addCategory(Intent.CATEGORY_DEFAULT);intent.setType("text/plain");PackageManager pm=this.getPackageManager();return pm.queryIntentActivities(intent,PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);}
通过自定义筛选就能实现自己想要的结果


原创粉丝点击