谨记(指定选择器 Intent.createChooser())
开始今天的内容前,先闲聊一下:
(1)突然有一天头脑风暴,对很多问题有了新的看法和见解,迫不及待的想要分享给大家,文档已经写好了,我需要通过微信或者 QQ,短信等社交工具发送给大家。
(2)在网上发现一段特别好的文章,想要保存收藏下来。
上面描述了进入智能手机时代后,我们经常遇到的两种情况,那么作为开发者的我们如何让自己开发的 APP 实现这两种功能呢,下面我们以实现文本的发送分享以及接收来梳理下两种功能的实现过程(其他类型的数据在博文末尾会给大家做简单介绍)。
在实现 APP 发送与分享的功能时,根据是否指定选择器 Intent.createChooser(),会有两种不同的实现效果。
每次需要发送分享的时候,都会弹出所有具有分享功能的 APP 供选择。(个人认为很人性化)
图中演示测试使用的安卓原生系统,在未设置选择器的时候,每次会提醒用户使用当前 APP 提交发送分享所使用的 APP 仅使用一次还是始终都使用(经测试万一大家手滑,误点了 "始终",那么好吧,如果下次想换其他 APP 分享内容时,除非你卸载重装当前 APP);但在其他一些安卓定制系统的品牌手机上测试时,发现仅第一次会跳出所有具有发送分享功能的 APP 供你选择(但是不会提示你仅使用一次还是始终),一旦选择后,后果与在原生系统上点击始终的效果相同。立马卸载 APP 的心都有了。
好了,实现效果大家都看到了,我们开始撸一把代码吧:
- 1
- 2 <LinearLayout
- 3 xmlns:android="http://schemas.android.com/apk/res/android"
- 4 xmlns:tools="http://schemas.android.com/tools"
- 5 android:id="@+id/activity_main"
- 6 android:layout_width="match_parent"
- 7 android:layout_height="match_parent"
- 8 android:orientation="vertical"
- 9 tools:context="com.example.administrator.sendshare.MainActivity">
- 10 <EditText
- 11 android:id="@+id/et"
- 12 android:layout_width="match_parent"
- 13 android:layout_height="wrap_content"
- 14 android:hint="想给潘侯爷说点什么"/>
- 15 <Button
- 16 android:layout_width="match_parent"
- 17 android:layout_height="wrap_content"
- 18 android:onClick="sendtext"
- 19 android:text="发送文本" />
- 20
注意注意:指定选择器啊
- 1 public class MainActivity extends AppCompatActivity {
- 2 EditText et; //声明文本编辑框
- 3 String str; //声明字符串,用于获取文本编辑框内的内容
- 4@Override 5 protected void onCreate(Bundle savedInstanceState) {
- 6 super.onCreate(savedInstanceState);
- 7 setContentView(R.layout.activity_main);
- 8 //获取文本框
- 9 et = (EditText) findViewById(R.id.et);
- 10
- }
- 11 //创建方法将输入的内容发出去
- 12 public void sendtext(View view) {
- 13 str = et.getText().toString();
- 14 Intent intent = new Intent();
- 15
- /*设置action为发送分享,
- 16 *并判断要发送分享的内容是否为空
- 17 */
- 18 intent.setAction(Intent.ACTION_SEND);
- 19
- if (str != null) {
- 20 intent.putExtra(Intent.EXTRA_TEXT, str);
- 21
- } else {
- 22 intent.putExtra(Intent.EXTRA_TEXT, "");
- 23
- }
- 24 intent.setType("text/plain"); //设置分享发送的数据类型
- 25 //未指定选择器,部分定制系统首次选择后,后期将无法再次改变
- 26 // startActivity(intent);
- 27 //指定选择器选择使用有发送文本功能的App
- 28 startActivity(Intent.createChooser(intent, getResources().getText(R.string.app_name)));
- 29
- }
- 30
- }
实现效果如下(将短信内容分享到我们的 APP 上):
layout 布局界面为初始默认,仅一个默认 helloworld 的 TextView 界面,这里就省略不写了。
- 1
- 2 3 package="com.example.administrator.test" >
- 4 <application
- 5 android:allowBackup="true"
- 6 android:icon="@mipmap/ic_launcher"
- 7 android:label="@string/app_name"
- 8 android:supportsRtl="true"
- 9 android:theme="@style/AppTheme" >
- 10
- 11
- 12
- 13 //添加接收文本用的action,category,mimeType
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 1 import android.content.Intent;
- 2 import android.support.v7.app.AppCompatActivity;
- 3 import android.os.Bundle;
- 4 import android.widget.TextView;
- 5 public class MainActivity extends AppCompatActivity {
- 6 private TextView tv;
- 7@Override 8 protected void onCreate(Bundle savedInstanceState) {
- 9 super.onCreate(savedInstanceState);
- 10 setContentView(R.layout.activity_main);
- 11 tv = (TextView) findViewById(R.id.tv);
- 12 //获取intent
- 13 Intent intent = getIntent();
- 14 String action = intent.getAction();
- 15 String type = intent.getType();
- 16 //设置接收类型为文本
- 17
- if (Intent.ACTION_SEND.equals(action) && type != null) {
- 18
- if ("text/plain".equals(type)) {
- 19 handlerText(intent);
- 20
- }
- 21
- }
- 22
- }
- 23 //该方法用于获取intent所包含的文本信息,并显示到APP的Activity界面上
- 24 private void handlerText(Intent intent) {
- 25 String data = intent.getStringExtra(Intent.EXTRA_TEXT);
- 26 tv.setText(data);
- 27
- }
- 28
- }
- 1 import android.content.Intent;
- 2 import android.support.v7.app.AppCompatActivity;
- 3 import android.os.Bundle;
- 4 import android.view.View;
- 5 public class Main2Activity extends AppCompatActivity {
- 6@Override 7 protected void onCreate(Bundle savedInstanceState) {
- 8 super.onCreate(savedInstanceState);
- 9 setContentView(R.layout.activity_main2);
- 10
- }
- 11 public void select(View view) {
- 12 Intent intent = new Intent();
- 13 intent.setAction(Intent.ACTION_SET_WALLPAPER);
- 14 startActivity(Intent.createChooser(intent, getResources().getText(R.string.app_name)));
- 15 // startActivity(intent);
- 16
- }
- 17
- }
- 1 public void sendimage(View view) {
- 2 Intent intent = new Intent();
- 3 intent.setAction(Intent.ACTION_SEND);
- 4 intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory() + "/DCIM/Camera/20161204_195228.jpg"));
- 5 intent.setType("image/*");
- 6 // startActivity(intent);
- 7 startActivity(Intent.createChooser(intent, getResources().getText(R.string.app_name)));
- 8
- }
- 1 public void sendimages(View view) {
- 2 ArrayList uris = new ArrayList < >();
- 3 //演示发送两张图片
- 4 uris.add(Uri.parse(Environment.getExternalStorageDirectory() + "/DCIM/Camera/20161204_195228.jpg"));
- 5 uris.add(Uri.parse(Environment.getExternalStorageDirectory() + "/DCIM/Camera/20161204_195155.jpg"));
- 6 Intent intent = new Intent();
- 7 intent.setAction(Intent.ACTION_SEND_MULTIPLE);
- 8 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
- 9 intent.setType("image/*");
- 10 // startActivity(intent);
- 11 startActivity(Intent.createChooser(intent, getResources().getText(R.string.app_name)));
- 12
- }
今天到这了,有问题欢迎评论讨论,晚安喽!
来源: