谨记(指定选择器Intent.createChooser())

开始今天的内容前,先闲聊一下:

(1)突然有一天头脑风暴,对很多问题有了新的看法和见解,迫不及待的想要分享给大家,文档已经写好了,我需要通过微信或者QQ,短信等社交工具发送给大家。

(2)在网上发现一段特别好的文章,想要保存收藏下来。

上面描述了进入智能手机时代后,我们经常遇到的两种情况,那么作为开发者的我们如何让自己开发的APP实现这两种功能呢,下面我们以实现文本的发送分享以及接收来梳理下两种功能的实现过程(其他类型的数据在博文末尾会给大家做简单介绍)。

第一种情况:APP实现发送分享文本功能

在实现APP发送与分享的功能时,根据是否指定选择器Intent.createChooser(),会有两种不同的实现效果。

(1)指定选择器的实现效果如下:

每次需要发送分享的时候,都会弹出所有具有分享功能的APP供选择。(个人认为很人性化)

(2)未指定选择器的实现效果如下:

图中演示测试使用的安卓原生系统,在未设置选择器的时候,每次会提醒用户使用当前APP提交发送分享所使用的APP仅使用一次还是始终都使用(经测试万一大家手滑,误点了“始终”,那么好吧,如果下次想换其他APP分享内容时,除非你卸载重装当前APP);但在其他一些安卓定制系统的品牌手机上测试时,发现仅第一次会跳出所有具有发送分享功能的APP供你选择(但是不会提示你仅使用一次还是始终),一旦选择后,后果与在原生系统上点击始终的效果相同。立马卸载APP的心都有了。

好了,实现效果大家都看到了,我们开始撸一把代码吧:

第一步:Layout中界面布局文件activity_main.xml文件(文本编辑框以及按钮):

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.sendshare.MainActivity">
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="想给潘侯爷说点什么"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="sendtext"
android:text="发送文本" />
</LinearLayout>

第二步:Java中实现代码MainActivity.java功能实现代码:

注意注意:指定选择器啊

 public class MainActivity extends AppCompatActivity {
EditText et;//声明文本编辑框
String str;//声明字符串,用于获取文本编辑框内的内容
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取文本框
et = (EditText) findViewById(R.id.et);
}
//创建方法将输入的内容发出去
public void sendtext(View view){
str=et.getText().toString();
Intent intent = new Intent();
/*设置action为发送分享,
*并判断要发送分享的内容是否为空
*/
intent.setAction(Intent.ACTION_SEND);
if(str!=null){
intent.putExtra(Intent.EXTRA_TEXT,str);
}else{
intent.putExtra(Intent.EXTRA_TEXT,"");
}
intent.setType("text/plain");//设置分享发送的数据类型
//未指定选择器,部分定制系统首次选择后,后期将无法再次改变
// startActivity(intent);
//指定选择器选择使用有发送文本功能的App
startActivity(Intent.createChooser(intent,getResources().getText(R.string.app_name)));
}
}

第二种情况:APP实现接收分享文本功能

实现效果如下(将短信内容分享到我们的APP上):

layout布局界面为初始默认,仅一个默认helloworld的TextView界面,这里就省略不写了。

第一步:AndroidMainfest.xml配置文件(添加接收文本所需的action等intent属性)

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.test" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
//添加接收文本用的action,category,mimeType
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

第二步:Java中实现代码MainActivity.java功能实现代码

 import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
//获取intent
Intent intent =getIntent();
String action = intent.getAction();
String type = intent.getType();
//设置接收类型为文本
if (Intent.ACTION_SEND.equals(action) && type != null){
if ("text/plain".equals(type)) {
handlerText(intent);
}
}
}
//该方法用于获取intent所包含的文本信息,并显示到APP的Activity界面上
private void handlerText(Intent intent) {
String data = intent.getStringExtra(Intent.EXTRA_TEXT);
tv.setText(data);
}
}

额外补充:

设置更新桌面背景,核心代码如下:

 import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
public void select(View view){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SET_WALLPAPER);
startActivity(Intent.createChooser(intent,getResources().getText(R.string.app_name)));
// startActivity(intent);
}
}

以二进制的形式分享发送图片,核心代码如下:

 public void sendimage(View view) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory()+"/DCIM/Camera/20161204_195228.jpg"));
intent.setType("image/*");
// startActivity(intent);
startActivity(Intent.createChooser(intent,getResources().getText(R.string.app_name)));
}

发送分享多张图片,核心代码如下:

 public void sendimages(View view) {
ArrayList<Uri> uris = new ArrayList<>();
//演示发送两张图片
uris.add(Uri.parse(Environment.getExternalStorageDirectory()+"/DCIM/Camera/20161204_195228.jpg"));
uris.add(Uri.parse(Environment.getExternalStorageDirectory()+"/DCIM/Camera/20161204_195155.jpg"));
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,uris);
intent.setType("image/*");
// startActivity(intent);
startActivity(Intent.createChooser(intent,getResources().getText(R.string.app_name)));
}

今天到这了,有问题欢迎评论讨论,晚安喽!

Android中实现APP文本内容的分享发送与接收方法简述的更多相关文章

  1. 【聊技术】在Android中实现自适应文本大小显示

    本周的聊技术话题和大家说说如何在Android中实现自适应文本大小显示. 想象一下,在布局中,通常显示文本的区域大小是固定的,但是文本长度并不总是固定的.比如列表中的文章标题.界面下方的按钮文本等等. ...

  2. Android 中如何计算 App 的启动时间?

    (转载) 已知的两种方法貌似可以获取,但是感觉结果不准确:一种是,adb shell am start -w packagename/activity,这个可以得到两个值,ThisTime和Total ...

  3. Android中为APP创建快捷方式的原理(自己的理解)

    我们首先来看Android中为APP创建快捷方式的原理: 从图上可以看出,Android大致分7步完成快捷方式的创建: 第一步:Android系统的launcher程序会调用它的pickShortcu ...

  4. 在Android中使App高速、简单地支持新浪微博、微信、QQ、facebook等十几个主流社交平台的分享功能

    前言 在如今的APP或者游戏中,分享功能差点儿已经成为标配.分享功能不但能够满足用户的需求.也能够为产品带来很多其它的用户,甚至能够对用户的行为.活跃度.年龄段等情况进行数据统计,使得软件公司能够对产 ...

  5. 用jQuery向div中添加Html文本内容

    前台代码: <link href="http://www.cnblogs.com/Content/themes/base/jquery-ui.css" rel="s ...

  6. Android中使用异步线程更新UI视图的几种方法

    在Android中子线程是不能更新ui的. 所以我们要通过其他方式来动态改变ui视图, 1.runOnUiThreadactivity提供的一个轻量级更新ui的方法,在Fragment需要使用的时候要 ...

  7. 关于import android.support.v4.app.ContextCompat;找不到contextcompat的解决方法

    android迁移到了androidx,那么相关库的import就有问题了,需要转变为androidx的,这里比如 import android.support.v4.app.ContextCompa ...

  8. 手把手教你Android手机与BLE终端通信--连接,发送和接收数据

    假设你还没有看上一篇 手把手教你Android手机与BLE终端通信--搜索,你就先看看吧,由于这一篇要接着讲搜索到蓝牙后的连接.和连接后的发送和接收数据. 评论里有非常多人问假设一条信息特别长,怎么不 ...

  9. Android测试(一):在Android中测试App

    原文:https://developer.android.com/training/testing/index.html 测试你的App是开发过程中的重要组成部分.通过对应用程序持续的运行测试,你可以 ...

随机推荐

  1. 如何一步一步用DDD设计一个电商网站(三)—— 初涉核心域

    一.前言 结合我们本次系列的第一篇博文中提到的上下文映射图(传送门:如何一步一步用DDD设计一个电商网站(一)—— 先理解核心概念),得知我们这个电商网站的核心域就是销售子域.因为电子商务是以信息网络 ...

  2. 使用JSONObject.fromObject的时候出现“There is a cycle in the hierarchy”异常 的解决办法

    在使用JSONObject.fromObject的时候,出现“There is a cycle in the hierarchy”异常.   意思是出现了死循环,也就是Model之间有循环包含关系: ...

  3. 使用Oracle官方巡检工具ORAchk巡检数据库

    ORAchk概述 ORAchk是Oracle官方出品的Oracle产品健康检查工具,可以从MOS(My Oracle Support)网站上下载,免费使用.这个工具可以检查Oracle数据库,Gold ...

  4. 8、Struts2 运行流程分析

    1.流程分析: 请求发送给 StrutsPrepareAndExecuteFilter StrutsPrepareAndExecuteFilter 询问 ActionMapper: 该请求是否是一个 ...

  5. 谱聚类(spectral clustering)原理总结

    谱聚类(spectral clustering)是广泛使用的聚类算法,比起传统的K-Means算法,谱聚类对数据分布的适应性更强,聚类效果也很优秀,同时聚类的计算量也小很多,更加难能可贵的是实现起来也 ...

  6. 《JavaScript设计模式与开发实践》整理

    最近在研读一本书<JavaScript设计模式与开发实践>,进阶用的. 一.高阶函数 高阶函数是指至少满足下列条件之一的函数. 1. 函数可以作为参数被传递. 2. 函数可以作为返回值输出 ...

  7. vscode 1.5安装体验

    1.下载安装 官方下载地址: http://code.visualstudio.com/ 界面截图: 2.图标显示功能File Icon Themes vscode1.5版本文件夹视图,可显示文件类型 ...

  8. stringstream的基本用法

    原帖地址:https://zhidao.baidu.com/question/580048330.htmlstringstream是字符串流.它将流与存储在内存中的string对象绑定起来.在多种数据 ...

  9. ActiveRecord模式整理

    DAO Data Access Object,数据访问对象 DAO是一个软件设计的指导原则,在核心J2EE模式中是这样介绍DAO模式的:为了建立一个健壮的J2EE应用,应该将所有对数据源的访问操作抽象 ...

  10. 仿陌陌的ios客户端+服务端源码项目

    软件功能:模仿陌陌客户端,功能很相似,注册.登陆.上传照片.浏览照片.浏览查找附近会员.关注.取消关注.聊天.语音和文字聊天,还有拼车和搭车的功能,支持微博分享和查找好友. 后台是php+mysql, ...