Android开发之分享功能(一)

2016-08-17  本文已影响0人  g000t

1.实现系统自带的分享功能

只安装了微信的手机

只有微信存在
新安装了新浪微博的手机
微信和微博同时存在
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("image/*);
    intent.putExtra(Intent.EXTRA_SUBJECT,"share");
    intent.putExtra(Intent.EXTRA_TEXT,"successfully");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(Intent.createChooser(intent,getTitle()));
Intent.ACTION_SEND

Activity Action: Deliver some data to someone else. Who the data is being delivered to is not specified; it is up to the receiver of this action to ask the user where the data should be sent.
When launching a SEND intent, you should usually wrap it in a chooser (through createChooser(Intent, CharSequence)), which will give the proper interface for the user to pick how to send your data and allow you to specify a prompt indicating what they are doing.
Input: getType() is the MIME type of the data being sent. get*Extra can have either a EXTRA_TEXT or EXTRA_STREAM field, containing the data to be sent. If using EXTRA_TEXT, the MIME type should be "text/plain"; otherwise it should be the MIME type of the data in EXTRA_STREAM. Use / if the MIME type is unknown (this will only allow senders that can handle generic data streams). If using EXTRA_TEXT, you can also optionally supply EXTRA_HTML_TEXT for clients to retrieve your text with HTML formatting.
As of JELLY_BEAN, the data being sent can be supplied through setClipData(ClipData). This allows you to use FLAG_GRANT_READ_URI_PERMISSION when sharing content: URIs and other advanced features of ClipData. If using this approach, you still must supply the same data through the EXTRA_TEXT or EXTRA_STREAM fields described below for compatibility with old applications. If you don't set a ClipData, it will be copied there for you when calling startActivity(Intent).
Optional standard extras, which may be interpreted by some recipients as appropriate, are: EXTRA_EMAIL, EXTRA_CC, EXTRA_BCC, EXTRA_SUBJECT.
Output: nothing.
Constant Value: "android.intent.action.SEND"

intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,"分享的内容");

分享单张图片时调用:

String imagePath = Environment.getExternalStorageDirectory()+File.separator+"test.jpg";
//得到文件的uri
Uri imageUri = Uri.fromFile(new File(imagePath));
//用Intent.ACTION_SEND 创建intent对象
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM,imageUri);

什么是URI?:

URI百度百科

在电脑术语中,统一资源标识符(Uniform Resource Identifier,或URI)是一个用于标识某一互联网资源名称的字符串。 该种标识允许用户对任何(包括本地和互联网)的资源通过特定的协议进行交互操作。URI由包括确定语法和相关协议的方案所定义。
Web上可用的每种资源 -HTML文档、图像、视频片段、程序等 - 由一个通用资源标识符(Uniform Resource Identifier, 简称"URI")
进行定位。

startActivity(Intent.createChooser(intent,getTitle()));

上面这句代码创建了一个Chooser

什么是Chooser?

这里的Intent.createChooser 实际上是创建一个ACTION_CHOOSER
在要分享数据时,会打开一个dialog,让用户去选择要分享到哪个app。

分享功能的实质—app之间的交互

通过Intent来发送一些请求,调用相关的应用来处理这些请求,那么相关的应用是怎么知道我们要给它传递数据呢?

<intent-filter>
<action android:name="com.example.project"/>
<action android:name="com.example.project"/>

所以在微博等APP 中要设置相应的intent-filter,能分享什么数据,那么就提供相应的标签去接收某种数据类型的数据。

我们自己的app就负责,设置好类型type 然后把它发送出去就行了。

上一篇下一篇

猜你喜欢

热点阅读