我是靠谱客的博主 兴奋冥王星,最近开发中收集的这篇文章主要介绍使自己的应用出现在分享列表,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

实现分享功能

    上面说的都是怎么调用分享功能,以下就开始写怎么实现分享功能,让我们的应用也出现在分享列表中。前面也说了分享功能是使用隐式调用Activtiy实现的,Activity需要声明 <intent-filter> 。


声明intent-filter

[java] view plain copy print ? 在code上查看代码片 派生到我的代码片
  1. <activity  
  2.            android:name="com.example.sharedemo.ShareActivity"  
  3.            android:label="@string/app_name" >  
  4.            <intent-filter>  
  5.                <action android:name="android.intent.action.SEND" />  
  6.   
  7.                <category android:name="android.intent.category.DEFAULT" />  
  8.   
  9.                <data android:mimeType="image/*" />  
  10.            </intent-filter>  
  11.            <intent-filter>  
  12.                <action android:name="android.intent.action.SEND" />  
  13.   
  14.                <category android:name="android.intent.category.DEFAULT" />  
  15.   
  16.                <data android:mimeType="text/plain" />  
  17.            </intent-filter>  
  18.            <intent-filter>  
  19.                <action android:name="android.intent.action.SEND_MULTIPLE" />  
  20.   
  21.                <category android:name="android.intent.category.DEFAULT" />  
  22.   
  23.                <data android:mimeType="image/*" />  
  24.            </intent-filter>  
  25.        </activity>  

上面声明了三种intent-filter,当然可以更多,这里只是举个例子,


处理接收数据

声明了intent-filter,响应的Activity就要处理响应的数据,示例如下:

[java] view plain copy print ? 在code上查看代码片 派生到我的代码片
  1. public class ShareActivity extends Activity{  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         // TODO Auto-generated method stub  
  6.         super.onCreate(savedInstanceState);  
  7.           
  8.         // Get intent, action and MIME type  
  9.         Intent intent = getIntent();  
  10.         String action = intent.getAction();  
  11.         String type = intent.getType();  
  12.   
  13.         if (Intent.ACTION_SEND.equals(action) && type != null) {  
  14.             if ("text/plain".equals(type)) {  
  15.                 handleSendText(intent); // Handle text being sent  
  16.             } else if (type.startsWith("image/")) {  
  17.                 handleSendImage(intent); // Handle single image being sent  
  18.             }  
  19.         } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {  
  20.             if (type.startsWith("image/")) {  
  21.                 handleSendMultipleImages(intent); // Handle multiple images being sent  
  22.             }  
  23.         } else {  
  24.             // Handle other intents, such as being started from the home screen  
  25.         }  
  26.     }  
  27.   
  28.     void handleSendText(Intent intent) {  
  29.         String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);  
  30.         String sharedTitle = intent.getStringExtra(Intent.EXTRA_TITLE);  
  31.         if (sharedText != null) {  
  32.             // Update UI to reflect text being shared  
  33.         }  
  34.     }  
  35.   
  36.     void handleSendImage(Intent intent) {  
  37.         Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);  
  38.         if (imageUri != null) {  
  39.             // Update UI to reflect image being shared  
  40.         }  
  41.     }  
  42.   
  43.     void handleSendMultipleImages(Intent intent) {  
  44.         ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);  
  45.         if (imageUris != null) {  
  46.             // Update UI to reflect multiple images being shared  
  47.         }  
  48.     }  
  49. }  

通过声明intent-filter,处理接受到的数据就能完成分享的接收功能。


更多

    上面只做了分享功能简单的说明,伴随着Android api的升级,也出现了一些新的完成“分享”功能的方法,比如 ShareActionProvider ,更多请参考。

最后

以上就是兴奋冥王星为你收集整理的使自己的应用出现在分享列表的全部内容,希望文章能够帮你解决使自己的应用出现在分享列表所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部