Android应用中使用及实现系统“分享”接口
为了应用的推广、传播,很多的应用中都有“分享”功能,一个按钮,点击后会出现短信、微博等等一切实现了分享功能的应用列表。这一篇文章主要介绍怎么调用分享功能和怎么实现分享接口让自己应用出现分享列表中。Android应用中能很方便的完成这些功能,这也正是Android的伟大之处,他能很简单的完成应用之间的沟通以相互整合。
调用分享功能
1、分享文本
分享功能使用的隐式启动Activity的方法,这里的Action使用的是 ACTION_SEND。
- Intent sendIntent = new Intent();
- sendIntent.setAction(Intent.ACTION_SEND);
- sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
- sendIntent.setType("text/plain");
- startActivity(sendIntent);
效果如下图的图一。
2、改变分享列表标题
使用上面的分享方式分享列表标题为“使用一下内容完成操作”,Android中提供了Intent.createChooser() ,这样能一直显示分享选择列表,并且修改了分享列表标题内容。
- Intent sendIntent = new Intent();
- sendIntent.setAction(Intent.ACTION_SEND);
- sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
- sendIntent.setType("text/plain");
- startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
使用Intent.createChooser()的好处:
If you callIntent.createChooser() for the intent, Android will always display the chooser. This has some advantages:
- Even if the user has previously selected a default action for this intent, the chooser will still be displayed.
- If no applications match, Android displays a system message.
- You can specify a title for the chooser dialog.
分享功能不只是Intent.EXTRA_TEXT,还可以 EXTRA_EMAIL, EXTRA_CC, EXTRA_BCC,EXTRA_SUBJECT. 只需要接受方完成响应数据接受。
3、分享图片
分享功能还支持二进制内容(Binary Content),但是多数是处理的图片,因为shareIntent.setType("image/jpeg")这一项设置了内容类型。可也以是其他类型,需要接受方支持。
- Intent shareIntent = new Intent();
- shareIntent.setAction(Intent.ACTION_SEND);
- shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
- shareIntent.setType("image/jpeg");
- startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
4、分享图片列表
分享功能不仅支持单张图片,还支持图片列表,这里还是说的范围太窄了,应该声明不仅仅是图片。
- ArrayList<Uri> imageUris = new ArrayList<Uri>();
- imageUris.add(imageUri1); // Add your image URIs here
- imageUris.add(imageUri2);
- Intent shareIntent = new Intent();
- shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
- shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
- shareIntent.setType("image/*");
- startActivity(Intent.createChooser(shareIntent, "Share images to.."));
实现分享功能
上面说的都是怎么调用分享功能,以下就开始写怎么实现分享功能,让我们的应用也出现在分享列表中。前面也说了分享功能是使用隐式调用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 ,更多请参考。
参考:
http://developer.android.com/training/sharing/index.html
Android应用中使用及实现系统“分享”接口的更多相关文章
- Android应用中实现系统“分享”接口
在android下各种文件管理器中,我们选择一个文件,点击分享可以看到弹出一些app供我们选择,这个是android系统分享功能,我们做的app也可以出现在这个列表中. 第一步:在Manifest.x ...
- 在Android Studio中使用shareSDK进行社会化分享(图文教程)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- Android Intent和IntentFilter详解与使用及实现系统“分享”接口
Intent Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到 ...
- Android开发中Parcelable接口的使用方法
在网上看到很多Android初入门的童鞋都在问Parcelable接口的使用方法,小编参考了相关Android教程,看到里面介绍的序列化方法主要有两种分别是实现Serializable接口和实现Par ...
- android - 调用系统分享功能分享图片
step1: 编写分享代码, 将Uri的生成方式改为由FileProvider提供的临时授权路径,并且在intent中添加flag 注意:在Android7.0之后,调用系统分享,传入URI的时候可能 ...
- 我的Android进阶之旅------>如何获取系统中定义了那些权限
在Window控制台中输入如下命令可以看到Android系统中列出的所有权限(如果自定义权限注册成功,在这里也会找到这些自定义的权限) adb shell pm list permissions C: ...
- android开发中系统自带语音模块的使用
android开发中系统自带语音模块的使用需求:项目中需要添加语音搜索模块,增加用户体验解决过程:在网上搜到语音搜索例子,参考网上代码,加入到了自己的项目,完成产品要求.这个问题很好解决,网上能找到很 ...
- Android开发中怎样调用系统Email发送邮件(多种调用方式)
在Android中调用其他程序进行相关处理,几乎都是使用的Intent,所以,Email也不例外,所谓的调用Email,只是说Email可以接收Intent并做这些事情 我们都知道,在Android中 ...
- unity3d开发的android应用中增加AD系统的详细步骤
unity3d开发的android应用中增加AD系统的详细步骤 博客分类: Unity3d unity3d Unity3d已经支持android,怎样在程序里增加admob? 试了一下,确实能够, ...
随机推荐
- JavaScript、for循环语句知识巩固,while(){}语句以及do{}while()语句以及switch()语句
一.for循环语句练习 关于for循环存在的两个问题类型 穷举:在不知道什么情况下才真的寻要我们的结果,自能让我们一个个走一遍. 迭代:在现有的条件根据规律不断求解,中间情况,最终推测出来的结果 1. ...
- webview长按事件js监听
做app开发时,用到了webview,需要监听webview的长按事件,使用原生的js处理监听如下: ; //定时器 //开始按 function gtouchstart() { timeOutEve ...
- 使用Jsoup 爬取网易首页所有的图片
package com.enation.newtest; import java.io.File; import java.io.FileNotFoundException; import java. ...
- Linux内核--异常和中断的区别
相信大家都知道非常著名的两个名词:异常和中断,不过,你真的理解这两个名词在说什么吗?它们之间有什么区别呢? 1.中断 大家都知道,当我们在敲击键盘的同时就会产生中断 ...
- Notes for Linux Administration Handbook (1) : Booting and Shutting Down
- android 平台搭建
直接到官网下载 http://developer.android.com/sdk/index.html?utm_source=weibolife
- 更多文章请访问"程序旅途”
更多文章请访问我的个人独立博客 程序旅途
- 为什么你还在用嵌入式的方式来使用mod_wsgi?
可能你还不知道你的python 网站是否跑在embedded mod of mod_wsgi,不管你知不知道请看下去,因为大部分人都是在这个工作模式下运行的.嵌入式顾名思义就是运行在apache的子 ...
- 【linux】修改文件所属用户和组
使用chown命令可以修改文件或目录所属的用户: 命令:chown 用户 目录或文件名 例如:chown qq /home/qq (把home目录下的qq目录的拥有者改为qq用户) 使用chgrp命 ...
- 老王教您怎么做cass7.1 8.0 9.1所有系列的复制狗 加密狗 破解狗
cass7.1 8.0 9.1所有系列的复制狗 加密狗 破解狗本来是出于好奇看到网上有这样的东西,学了下感觉挺简单的,如果你是cass的初学者想仅仅是想学习这个软件,不想花大价格购买正版的,这个是可以 ...