android分享功能以及将自己的应用添加到分享列表中(2)

在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);  
}  
通过自定义筛选就能实现自己想要的结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值