在Android系统中如何给应用增加分享功能,怎样将应用加入系统的分享选择列表?
在Android系统中如何给应用增加分享功能
Intent.createChooser()方法用来弹出系统分享列表。
createChooser方法接受Intent做参数,也同时接纳了Intent里面要求的filter(ACTION_SEND),只有支持ACTION_SEND的Activity才会被列入可选列表
查看Intent对应的组件是否存在,可查看Android判断Intent是否存在,是否可用
[java] view plaincopy
<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>
通过上面的方法就可以实现分享功能了,用户可以根据自己的需求随意添加点击来触发这个事件!
怎样将自己的应用加入系统的分享选择列表?
先看一个腾讯微博的例子(网友反编译后的例子)
[html] view plaincopy
<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>
通过上面的可以看出下面这些是关键:
[html] view plaincopy
<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>
那我们就在我们的程序中添加相应的代码即可以实现
[java] view plaincopy
<data android:mimeType="image/*" /> //可以是text/plain
如果自定义弹出列表的项(毕竟如果程序安装的多的话,会出现好长好长的列表)
[java] view plaincopy
/* 获得支持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);
}
通过自定义筛选就能实现自己想要的结果
android分享功能以及将自己的应用添加到分享列表中(2)
最新推荐文章于 2022-03-14 16:08:20 发布