概述
实现分享功能
上面说的都是怎么调用分享功能,以下就开始写怎么实现分享功能,让我们的应用也出现在分享列表中。前面也说了分享功能是使用隐式调用Activtiy实现的,Activity需要声明 <intent-filter>
。
声明intent-filter
- <activity
- android:name="com.example.sharedemo.ShareActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.SEND" />
- <category android:name="android.intent.category.DEFAULT" />
- <data android:mimeType="image/*" />
- </intent-filter>
- <intent-filter>
- <action android:name="android.intent.action.SEND" />
- <category android:name="android.intent.category.DEFAULT" />
- <data android:mimeType="text/plain" />
- </intent-filter>
- <intent-filter>
- <action android:name="android.intent.action.SEND_MULTIPLE" />
- <category android:name="android.intent.category.DEFAULT" />
- <data android:mimeType="image/*" />
- </intent-filter>
- </activity>
上面声明了三种intent-filter,当然可以更多,这里只是举个例子,
处理接收数据
声明了intent-filter,响应的Activity就要处理响应的数据,示例如下:
- public class ShareActivity extends Activity{
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- // Get intent, action and MIME type
- Intent intent = getIntent();
- String action = intent.getAction();
- String type = intent.getType();
- if (Intent.ACTION_SEND.equals(action) && type != null) {
- if ("text/plain".equals(type)) {
- handleSendText(intent); // Handle text being sent
- } else if (type.startsWith("image/")) {
- handleSendImage(intent); // Handle single image being sent
- }
- } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
- if (type.startsWith("image/")) {
- handleSendMultipleImages(intent); // Handle multiple images being sent
- }
- } else {
- // Handle other intents, such as being started from the home screen
- }
- }
- void handleSendText(Intent intent) {
- String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
- String sharedTitle = intent.getStringExtra(Intent.EXTRA_TITLE);
- if (sharedText != null) {
- // Update UI to reflect text being shared
- }
- }
- void handleSendImage(Intent intent) {
- Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
- if (imageUri != null) {
- // Update UI to reflect image being shared
- }
- }
- void handleSendMultipleImages(Intent intent) {
- ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
- if (imageUris != null) {
- // Update UI to reflect multiple images being shared
- }
- }
- }
通过声明intent-filter,处理接受到的数据就能完成分享的接收功能。
更多
上面只做了分享功能简单的说明,伴随着Android api的升级,也出现了一些新的完成“分享”功能的方法,比如 ShareActionProvider
,更多请参考。
最后
以上就是兴奋冥王星为你收集整理的使自己的应用出现在分享列表的全部内容,希望文章能够帮你解决使自己的应用出现在分享列表所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复