使自己的应用出现在分享列表

实现分享功能

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


声明intent-filter

  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就要处理响应的数据,示例如下:

  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 ,更多请参考。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值