Android Studio App开发入门之在活动之间传递消息(附源码 超详细必看)(包括显示和隐式Intent,向上一个和下一个Activity发送数据)

简介: Android Studio App开发入门之在活动之间传递消息(附源码 超详细必看)(包括显示和隐式Intent,向上一个和下一个Activity发送数据)

运行有问题或需要源码请点赞关注收藏后评论区留言~~

显示Intent和隐式Intent

Intent是各个组件之间的信息沟通的桥梁,既能在Activity之间沟通,又能在Activity与Service沟通,也能在Activtiy与Broadcast之间沟通,总而言之,Intent用于Android各组件之间的通信 它主要处理以下三部分工作

1:表明本次通信请求从哪里来  到哪里去 要怎么走

2:发起方携带本次通信需要的数据内容

3:发起方若想判断接收方的处理结果 Intent就要负责让接收方传回应答的数据内容

1:显示Intent 直接指定来源活动与目标活动 属于精确匹配

2:隐式Intent 没有明确地指定要跳转的目标活动 只给出一个动作字符串让系统自动匹配,属于模糊匹配

下面实现一个简单的调用系统拨号程序的例子 效果如下

ActionUriActivity类代码如下

package com.example.chapter04;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class ActionUriActivity extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action_uri);
        findViewById(R.id.btn_dial).setOnClickListener(this);
        findViewById(R.id.btn_sms).setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        String phoneNo = "12345";
        if (v.getId() == R.id.btn_dial) { // 点击了“跳到拨号页面”按钮
            Intent intent = new Intent(); // 创建一个新意图
            intent.setAction(Intent.ACTION_DIAL); // 设置意图动作为准备拨号
            Uri uri = Uri.parse("tel:" + phoneNo); // 声明一个拨号的Uri
            intent.setData(uri); // 设置意图前往的路径
            startActivity(intent); // 启动意图通往的活动页面
        } else if (v.getId() == R.id.btn_sms) { // 点击了“跳到短信页面”按钮
            Intent intent = new Intent(); // 创建一个新意图
            intent.setAction(Intent.ACTION_SENDTO); // 设置意图动作为发短信
            Uri uri = Uri.parse("smsto:" + phoneNo); // 声明一个发送短信的Uri
            intent.setData(uri); // 设置意图前往的路径
            startActivity(intent); // 启动意图通往的活动页面
        }
    }
}

activity_action_uriXML文件代码如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="点击以下按钮将向号码12345发起请求"
        android:textColor="@color/black"
        android:textSize="17sp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_dial"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="跳到拨号页面"
            android:textColor="@color/black"
            android:textSize="17sp" />
        <Button
            android:id="@+id/btn_sms"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="跳到短信页面"
            android:textColor="@color/black"
            android:textSize="17sp" />
    </LinearLayout>
</LinearLayout>

向下一个Activity发送数据

Android引入了Bundle概念,可以把Bundle理解为超市的寄包柜或快递收件柜,大小包裹由Bundle统一存取。Bundle内部用于存放信息的数据结构是Map映射,既可添加或删除元素,还可以判断元素是否存在

Bundle对象操作各类型数据的读写方法说明如下图

 

ActSendActivity类代码如下

package com.example.chapter04;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.example.chapter04.util.DateUtil;
public class ActSendActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_send; // 声明一个文本视图对象
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_send);
        // 从布局文件中获取名叫tv_send的文本视图
        tv_send = findViewById(R.id.tv_send);
        findViewById(R.id.btn_send).setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_send) {
            // 创建一个意图对象,准备跳到指定的活动页面
            Intent intent = new Intent(this, ActReceiveActivity.class);
            Bundle bundle = new Bundle(); // 创建一个新包裹
            // 往包裹存入名叫request_time的字符串
            bundle.putString("request_time", DateUtil.getNowTime());
            // 往包裹存入名叫request_content的字符串
            bundle.putString("request_content", tv_send.getText().toString());
            intent.putExtras(bundle); // 把快递包裹塞给意图
            startActivity(intent); // 跳转到意图指定的活动页面
        }
    }
}

activity_act_sendXML文件代码如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/tv_send"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="今天的天气真不错"
        android:textColor="#000000"
        android:textSize="17sp" />
    <Button
        android:id="@+id/btn_send"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="发送以上文字"
        android:textColor="#000000"
        android:textSize="17sp" />
</LinearLayout>

ActReceiveActivity类代码如下

package com.example.chapter04;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class ActReceiveActivity extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_receive);
        // 从布局文件中获取名叫tv_receive的文本视图
        TextView tv_receive = findViewById(R.id.tv_receive);
        findViewById(R.id.btn_receive).setOnClickListener(this);
        // 从上一个页面传来的意图中获取快递包裹
        Bundle bundle = getIntent().getExtras();
        // 从包裹中取出名叫request_time的字符串
        String request_time = bundle.getString("request_time");
        // 从包裹中取出名叫request_content的字符串
        String request_content = bundle.getString("request_content");
        String desc = String.format("收到请求消息:\n请求时间为%s\n请求内容为%s",
                request_time, request_content);
        tv_receive.setText(desc); // 把请求消息的详情显示在文本视图上
    }
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_receive) {
            finish(); // 结束当前的活动页面
        }
    }
}

activity_act_receiveXML文件代码如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/tv_receive"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:textColor="#000000"
        android:textSize="17sp" />
    <Button
        android:id="@+id/btn_receive"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="知道了"
        android:textColor="#000000"
        android:textSize="17sp" />
</LinearLayout>

向上一个Activity返回数据

数据传递经常是相互的,上一个页面不但把请求数据发送到下一个页面,有时候还要处理下一个页面的应答数据,所谓应答发生在下一个页面返回到上一个页面之际。如果只把请求数据发送到下一个页面,上一个页面调用startActivity即可。如果还要处理下一个页面的应答数据,此时就得分多步处理 效果如下

ActRequestActivity类代码如下

package com.example.chapter04;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.chapter04.util.DateUtil;
public class ActRequestActivity extends AppCompatActivity implements View.OnClickListener {
    private String mRrequest = "你吃饭了吗?来我家吃吧";
    private TextView tv_response; // 声明一个文本视图对象
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_request);
        // 从布局文件中获取名叫tv_request的文本视图
        TextView tv_request = findViewById(R.id.tv_request);
        tv_request.setText("待发送的消息为:"+mRrequest);
        // 从布局文件中获取名叫tv_response的文本视图
        tv_response = findViewById(R.id.tv_response);
        findViewById(R.id.btn_request).setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_request) {
            // 创建一个意图对象,准备跳到指定的活动页面
            Intent intent = new Intent(this, ActResponseActivity.class);
            Bundle bundle = new Bundle(); // 创建一个新包裹
            // 往包裹存入名叫request_time的字符串
            bundle.putString("request_time", DateUtil.getNowTime());
            // 往包裹存入名叫request_content的字符串
            bundle.putString("request_content", mRrequest);
            intent.putExtras(bundle); // 把快递包裹塞给意图
            // 期望接收下个页面的返回数据。第二个参数为本次请求代码
            startActivityForResult(intent, 0);
        }
    }
    // 从下一个页面携带参数返回当前页面时触发。其中requestCode为请求代码,
    // resultCode为结果代码,intent为下一个页面返回的意图对象
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) { // 接收返回数据
        super.onActivityResult(requestCode, resultCode, intent);
        // 意图非空,且请求代码为之前传的0,结果代码也为成功
        if (intent!=null && requestCode==0 && resultCode== Activity.RESULT_OK) {
            Bundle bundle = intent.getExtras(); // 从返回的意图中获取快递包裹
            // 从包裹中取出名叫response_time的字符串
            String response_time = bundle.getString("response_time");
            // 从包裹中取出名叫response_content的字符串
            String response_content = bundle.getString("response_content");
            String desc = String.format("收到返回消息:\n应答时间为:%s\n应答内容为:%s",
                    response_time, response_content);
            tv_response.setText(desc); // 把返回消息的详情显示在文本视图上
        }
    }
}

activity_act_requestXML文件代码如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/tv_request"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:textColor="#000000"
        android:textSize="17sp" />
    <Button
        android:id="@+id/btn_request"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="传送请求数据"
        android:textColor="#000000"
        android:textSize="17sp" />
    <TextView
        android:id="@+id/tv_response"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:textColor="#000000"
        android:textSize="17sp" />
</LinearLayout>

ActResponseActivity类代码如下

package com.example.chapter04;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.chapter04.util.DateUtil;
public class ActResponseActivity extends AppCompatActivity implements View.OnClickListener {
    private String mResponse = "我吃过了,还是你来我家吃";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_response);
        // 从布局文件中获取名叫tv_request的文本视图
        TextView tv_request = findViewById(R.id.tv_request);
        findViewById(R.id.btn_response).setOnClickListener(this);
        // 从布局文件中获取名叫tv_response的文本视图
        TextView tv_response = findViewById(R.id.tv_response);
        tv_response.setText("待返回的消息为:"+mResponse);
        // 从上一个页面传来的意图中获取快递包裹
        Bundle bundle = getIntent().getExtras();
        // 从包裹中取出名叫request_time的字符串
        String request_time = bundle.getString("request_time");
        // 从包裹中取出名叫request_content的字符串
        String request_content = bundle.getString("request_content");
        String desc = String.format("收到请求消息:\n请求时间为:%s\n请求内容为:%s",
                request_time, request_content);
        tv_request.setText(desc); // 把请求消息的详情显示在文本视图上
    }
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_response) {
            Intent intent = new Intent(); // 创建一个新意图
            Bundle bundle = new Bundle(); // 创建一个新包裹
            // 往包裹存入名叫response_time的字符串
            bundle.putString("response_time", DateUtil.getNowTime());
            // 往包裹存入名叫response_content的字符串
            bundle.putString("response_content", mResponse);
            intent.putExtras(bundle); // 把快递包裹塞给意图
            // 携带意图返回上一个页面。RESULT_OK表示处理成功
            setResult(Activity.RESULT_OK, intent);
            finish(); // 结束当前的活动页面
        }
    }
}

activity_act_responseXML文件代码如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/tv_request"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:textColor="#000000"
        android:textSize="17sp" />
    <Button
        android:id="@+id/btn_response"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="返回应答数据"
        android:textColor="#000000"
        android:textSize="17sp" />
    <TextView
        android:id="@+id/tv_response"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:textColor="#000000"
        android:textSize="17sp" />
</LinearLayout>

创作不易 觉得有帮助请点赞关注收藏

相关文章
|
10天前
|
前端开发 Java Shell
【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
80 20
【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
|
7天前
|
Dart 前端开发 Android开发
【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
25 4
【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
|
30天前
|
Windows
【Azure App Service】对App Service中CPU指标数据中系统占用部分(System CPU)的解释
在Azure App Service中,CPU占比可在App Service Plan级别查看整个实例的资源使用情况。具体应用中仅能查看CPU时间,需通过公式【CPU Time / (CPU核数 * 60)】估算占比。CPU百分比适用于可横向扩展的计划(Basic、Standard、Premium),而CPU时间适用于Free或Shared计划。然而,CPU Percentage包含所有应用及系统占用的CPU,高CPU指标可能由系统而非应用请求引起。详细分析每个进程的CPU占用需抓取Windows Performance Trace数据。
83 40
|
26天前
|
Dart 前端开发 Android开发
【02】写一个注册页面以及配置打包选项打包安卓apk测试—开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈
【02】写一个注册页面以及配置打包选项打包安卓apk测试—开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈
32 1
【02】写一个注册页面以及配置打包选项打包安卓apk测试—开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈
|
2月前
|
存储 监控 API
app开发之安卓Android+苹果ios打包所有权限对应解释列表【长期更新】-以及默认打包自动添加权限列表和简化后的基本打包权限列表以uniapp为例-优雅草央千澈
app开发之安卓Android+苹果ios打包所有权限对应解释列表【长期更新】-以及默认打包自动添加权限列表和简化后的基本打包权限列表以uniapp为例-优雅草央千澈
79 11
|
3月前
|
XML 数据库 Android开发
探索Android开发:从入门到精通的旅程
在这篇文章中,我们将一起踏上一段激动人心的旅程,通过深入浅出的方式,解锁Android开发的秘密。无论你是编程新手还是有经验的开发者,本文都将为你提供宝贵的知识和技能,帮助你构建出色的Android应用。我们将从基础概念开始,逐步深入到高级技巧和最佳实践,最终实现从初学者到专家的转变。让我们开始吧!
69 3
|
4月前
|
存储 前端开发 测试技术
Android kotlin MVVM 架构简单示例入门
Android kotlin MVVM 架构简单示例入门
61 1
|
3月前
|
XML IDE Java
安卓应用开发入门:从零开始的旅程
【10月更文挑战第23天】本文将带领读者开启一段安卓应用开发的奇妙之旅。我们将从最基础的概念讲起,逐步深入到开发实践,最后通过一个简易的代码示例,展示如何将理论知识转化为实际的应用。无论你是编程新手,还是希望扩展技能的软件工程师,这篇文章都将为你提供有价值的指导和启发。
60 0
|
6月前
|
API 开发工具 Android开发
Android源码下载
Android源码下载
695 0
|
存储 Ubuntu Linux
linphone android sdk 源码下载编译
linphone android sdk 源码下载编译
1229 0
linphone android sdk 源码下载编译

热门文章

最新文章

  • 1
    如何修复 Android 和 Windows 不支持视频编解码器的问题?
  • 2
    当flutter react native 等混开框架-并且用vscode-idea等编译器无法打包apk,打包安卓不成功怎么办-直接用android studio如何打包安卓apk -重要-优雅草卓伊凡
  • 3
    Android经典面试题之Kotlin中Lambda表达式和匿名函数的区别
  • 4
    MNN-LLM App:在手机上离线运行大模型,阿里巴巴开源基于 MNN-LLM 框架开发的手机 AI 助手应用
  • 5
    【11】flutter进行了聊天页面的开发-增加了即时通讯聊天的整体页面和组件-切换-朋友-陌生人-vip开通详细页面-即时通讯sdk准备-直播sdk准备-即时通讯有无UI集成的区别介绍-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
  • 6
    【Azure App Service】基于Linux创建的App Service是否可以主动升级内置的Nginx版本呢?
  • 7
    微信小程序 app.json 配置文件解析与应用
  • 8
    【05】flutter完成注册页面完善样式bug-增加自定义可复用组件widgets-严格规划文件和目录结构-规范入口文件-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈
  • 9
    【Azure Function】Function App出现System.IO.FileNotFoundException异常
  • 10
    【Azure Logic App】使用MySQL 新增行触发器遇见错误 :“Unknown column 'created_at' in 'order clause'”