Android组件通信——Intent(二十三)

news2024/9/20 6:24:21

1. 认识Intent

1.1 知识点

(1)了解Intent的主要作用;

(2)掌握Activity程序对Intent操作的支持;

(3)可以使用Intent完成Activity程序间的跳转,也可以通过Intent接收返回数据

1.2 具体内容

以下用一个intent在Activity之间进行跳转的例子说明:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".IntentActivity" >

    <Button
        android:id="@+id/but_first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转到第二个Activity"
         />

</RelativeLayout>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".IntentActivity" >

    <TextView
        android:id="@+id/tv_second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       />

</RelativeLayout>

package com.example.intentproject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class IntentActivity extends Activity {
	Button but = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_intent_first);
		but = (Button) super.findViewById(R.id.but_first);
		but.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(IntentActivity.this,SecondActivity.class);//创建Intent对象,并指定跳转的Activity
				intent.putExtra("msg", "你好,我是来自毛栗子的第一个Activity");
				startActivity(intent);
			}
		});
	}

}

package com.example.intentproject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends Activity {
    TextView tv = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_intent_second);
		Intent intent = getIntent();//接收Intent对象
		String msg = intent.getStringExtra("msg");
		tv = (TextView) super.findViewById(R.id.tv_second);
		tv.setText(msg);//显示信息
	}


}

完成以上程序编写之后,不要忘记去主配文件当中去查看,新建的Activity是否已经注册。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".IntentActivity" >

    <Button
        android:id="@+id/but_first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转到第二个Activity"
         />
    <TextView 
        android:id="@+id/tv_first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/but_first"
        />

</RelativeLayout>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".IntentActivity" >

    <TextView
        android:id="@+id/tv_second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       />
    <Button 
        android:id="@+id/but_second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_second"
        android:text="回传数据到第一个Activity"
        />

</RelativeLayout>

package com.example.intentproject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class IntentActivity extends Activity {
	Button but = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_intent_first);
		but = (Button) super.findViewById(R.id.but_first);
		but.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(IntentActivity.this,SecondActivity.class);//创建Intent对象,并指定跳转的Activity
				intent.putExtra("msg", "你好,我是来自毛栗子的第一个Activity");
			    IntentActivity.this.startActivityForResult(intent, 1);//启动intent对象
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode,int resultCode,Intent data){
		switch(resultCode){
		    case RESULT_OK:
			     TextView tv = (TextView) super.findViewById(R.id.tv_first);
		         tv.setText(data.getStringExtra("returnmsg"));
		         break;
		    case RESULT_CANCELED:
		    	 break;
		    default:
		    	 break;
		}
	}

}

package com.example.intentproject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class SecondActivity extends Activity {
    TextView tv = null;
    Button but = null;
    Intent intent = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_intent_second);
		Intent intent = getIntent();//接收Intent对象
		String msg = intent.getStringExtra("msg");
		tv = (TextView) super.findViewById(R.id.tv_second);
		tv.setText(msg);//显示信息
		but = (Button) super.findViewById(R.id.but_second);
		but.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent =  SecondActivity.this.getIntent();
				intent.putExtra("returnmsg", "你好,是卡哇伊吗?");
				setResult(RESULT_OK,intent);
				finish();
			}
		});
	}


}

1.3 课程小结

(1)Intent可以用于多个Activity间的跳转操作;

(2)使用Intent也可以接收Activity回传的数据。

2. Intent深入

2.1 知识点

(1)掌握内置Intent的调用操作。

2.2 具体内容

通过Intent跳转到网页:

package com.example.intentproject;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class IntentActivity extends Activity {
	Button but = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_intent_first);
		but = (Button) super.findViewById(R.id.but_first);
		but.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Uri uri = Uri.parse("http://www.baidu.com");
				Intent it =new Intent();
				it.setAction(Intent.ACTION_VIEW);//指定action
			    it.setData(uri);//设置数据
			    startActivity(it);
			}
		});
	}
	
	

}

以下使用Intent跳转到拨号页面:

package com.example.intentproject;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class IntentActivity extends Activity {
	Button but = null;
	EditText edt = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_intent_first);
		but = (Button) super.findViewById(R.id.but_first);
		edt = (EditText) super.findViewById(R.id.edt);
		but.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String num = edt.getText().toString().trim();
				Uri uri = Uri.parse("tel:"+num);
				Intent it =new Intent();
				it.setAction(Intent.ACTION_CALL);//指定action
			    it.setData(uri);//设置数据
			    startActivity(it);
			}
		});
	}
	
	

}

拨号还需要进行权限的配置:

<uses-permission 
        android:name="android.permission.CALL_PHONE"
        />

通过intent发送短信:

package com.example.intentproject;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class IntentActivity extends Activity {
	Button but = null;
	EditText edt = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_intent_first);
		but = (Button) super.findViewById(R.id.but_first);
		edt = (EditText) super.findViewById(R.id.edt);
		but.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String num = edt.getText().toString().trim();
				String note ="这是短信的内容";
				Uri uri = Uri.parse("smsto:"+num);
				Intent it =new Intent();
				it.setAction(Intent.ACTION_SENDTO);//指定action
				it.putExtra("sms_body", note);//指定附加信息
				it.setType("vndd.android-dir/mms-sms");//设置MIME类型,对方手机将以短信形式打开我们这发生的信息
			    it.setData(uri);//设置数据
			    startActivity(it);
			}
		});
	}
	
	

}

2.3 小结

(1)在Android系统之中提供了多种Intent,用户只需要设置好URL以及附加的数据就可以完成这些Intent的操作。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1080991.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

QT基础 QChart绘制折线

目录 1.简单折线 2.数学折线 3.可滑动折线 1.简单折线 //![1] //! 折现段坐标QLineSeries *series new QLineSeries(); //![1]//![2] //! 添加点series->append(0, 6);series->append(2, 4);series->append(3, 8);series->append(7, 4);series->append(10, 5)…

YOLOv7改进: CFP:即插即用的多尺度融合模块,EVC助力小目标检测| 顶刊TIP 2023

💡💡💡本文独家改进:即插即用的多尺度融合模块,EVC助力小目标检测 EVC | 亲测在多个数据集实现暴力涨点,强烈推荐,独家首发; 收录: YOLOv7高阶自研专栏介绍:http://t.csdnimg.cn/tYI0c ✨✨✨前沿最新计算机顶会复现 🚀🚀🚀YOLOv7自研创新结合,轻松搞…

MS31804四通道低边驱动器可pin对pin兼容DRV8804

MS31804TE 是一个具有过流保护功能的四通道低边驱动器。MS31804TE 内置钳位二极管&#xff0c;用来钳制由电感负载续流产生的电压。MS31804TE 可以驱动单极步进电机、直流电机、继电器、螺线管或者其它负载。 散热良好的情况下&#xff0c;MS31804TE 可以提供每个通道最高 2A 的…

整理笔记——二极管

一、什么是二极管 二极管是一种由半导体材料制成的一种具有单向导电性能的电子元器件&#xff0c;二极管的核心是PN结。 加在二极管两端的电压和通关的电流被成为&#xff0c;二极管的伏安特性曲线 ​​​ 二极管的正向特性&#xff1a;起初正向电压较小时&#xff0c;正向电流…

[ACTF2020 新生赛]Exec1

拿到题目&#xff0c;不知道是sql注入还是命令执行漏洞 先ping一下主机 有回显&#xff0c;说明是命令执行漏洞 我们尝试去查看目录 127.0.0.1|ls&#xff0c;发现有回显&#xff0c;目录下面有个index.php的文件 我们之间访问index.php 输入127.0.0.1;cat index.php 发现又…

基于Springboot实现汽车租赁平台管理系统项目【项目源码】

基于Springboot实现汽车租赁平台管理系统演示 JAVA简介 Java主要采用CORBA技术和安全模型&#xff0c;可以在互联网应用的数据保护。它还提供了对EJB&#xff08;Enterprise JavaBeans&#xff09;的全面支持&#xff0c;java servlet API&#xff0c;JSP&#xff08;java ser…

选择适合自身业务的HTTP代理有哪些因素决定?

相信对很多爬虫工作者和数据采集的企业来说&#xff0c;如何选购适合自己业务的HTTP代理是一个特别特别困扰的选题&#xff0c;市面上那么多HTTP代理厂商&#xff0c;好像这家有这些缺点&#xff0c;转头又看到另外一家的缺点&#xff0c;要找一家心仪的仿佛大海捞针。今天我们…

Table.Group系列_第4参数为全局的情况下,利用第五参数进行分组汇总

原始数据: 部门与职位存在于同一列中 实现功能: 根据筛选条件,可对部门或职位进行统计汇总第一列列名根据筛选自动变更,显示当前统计的维度 实现方式: 1. 构建筛选器内容 在任意空白单元格内输入需要筛选的内容 2. 插入"组合框"控件,并进行相应设置 从开发工具…

flex布局在多层嵌套时,内层设置了justify-content: space-between;不生效问题

内层的地址和时间这一行&#xff0c;设置了justify-content: space-between;但并不生效&#xff0c;原因是要在上一层.center 设置 flex:1;&#xff08; 重点&#xff09; 经常忘记&#xff0c;特在此记录一下&#xff0c;以下是代码 <view class"index-card" c…

深度学习基础知识 BatchNorm、LayerNorm、GroupNorm的用法解析

深度学习基础知识 BatchNorm、LayerNorm、GroupNorm的用法解析 1、BatchNorm2、LayerNorm3、GroupNorm用法&#xff1a; BatchNorm、LayerNorm 和 GroupNorm 都是深度学习中常用的归一化方式。 它们通过将输入归一化到均值为 0 和方差为 1 的分布中&#xff0c;来防止梯度消失和…

学生用的台灯哪种比较好?分享专家推荐的学生台灯

对于学生来说&#xff0c;台灯是必不可少的一盏学习照明灯具&#xff0c;它能提供室内不足的光线、亮度&#xff0c;基本每个学生在宿舍、家里都会备着一台。不过台灯也并不是随便挑选一台使用就可以的&#xff0c;很多学生就是因为使用了一些价格低廉、质量安全没有保障的台灯…

【大数据】hadoop安装部署(学习笔记)

一、集群组成概述 Hadoop集群包括两个集群&#xff1a;HDFS集群、YARN集群 两个集群逻辑上分离、通常物理上在一起 两个集群都是标准的主从架构集群 HDFS集群&#xff08;分布式存储&#xff09;&#xff1a; 主角色&#xff1a;NameNode从角色&#xff1a;DataNode主角色…

代码随想录算法训练营第五十八天 |583. 两个字符串的删除操作、72. 编辑距离、编辑距离总结篇

一、583. 两个字符串的删除操作 题目链接/文章讲解/视频讲解&#xff1a;代码随想录 思考&#xff1a; 1.确定dp数组&#xff08;dp table&#xff09;以及下标的含义 dp[i][j]&#xff1a;以i-1为结尾的字符串word1&#xff0c;和以j-1位结尾的字符串word2&#xff0c;想要达…

postman 密码rsa加密登录-2加密密码

上一篇讲了获取公钥&#xff0c;将环境准备好之后&#xff0c;在登录接口的Pre-request Scrip 里&#xff0c;使用公钥进行加密后在正常登录。本文采用的方案是使用第三方模块forge.js来实现加密。 1、环境准备好&#xff0c;系统git 和node都OK。下载forge.js git clone htt…

Java-Atomic原子操作类详解及源码分析,Java原子操作类进阶,LongAdder源码分析

文章目录 一、Java原子操作类概述1、什么是原子操作类2、为什么要用原子操作类3、CAS入门 二、基本类型原子类1、概述2、代码实例 三、数组类型原子类1、概述2、代码实例 四、引用类型原子类1、概述2、AtomicReference3、ABA问题与AtomicStampedReference4、一次性修改&#xf…

SpringBoot (1)

目录 1 入门案例 1.1 环境准备 1.2 编写pom.xml 1.3 编写入口程序 1.4 编写接口 1.5 编写配置 1.6 快速部署 1.6.1 打jar包 1.6.2 部署 1.7 访问接口 2 全注解开发 2.1 常用注解 2.2 属性绑定注解 2.2.1 注册组件 2.2.2 ConfigurationProperties(prefix"te…

AlphaPose Pytorch 代码详解(一):predict

前言 代码地址&#xff1a;AlphaPose-Pytorch版 本文以图像 1.jpg&#xff08;854x480&#xff09;为例对整个预测过程的各个细节进行解读并记录 python demo.py --indir examples/demo --outdir examples/res --save_img1. YOLO 1.1 图像预处理 cv2读取BGR图像 img [480,…

哈希的应用--位图和布隆过滤器

哈希的应用--位图和布隆过滤器 位图1. 位图概念2. 位图在实际中的应用3. 位图相似应用给定100亿个整数&#xff0c;如何找到只出现一次的整数&#xff1f;1个文件100亿int&#xff0c;1G内存&#xff0c;如何找到不超过2次的所有整数 布隆过滤器1. 布隆过滤器的提出2. 布隆过滤…

HarmonyOS学习 -- ArkTS开发语言入门

文章目录 一、编程语言介绍二、TypeScript基础类型1. 布尔值2. 数字3. 字符串4. 数组5. 元组6. 枚举7. unknown8. void9. null 和 undefined10. 联合类型 三、TypeScript基础知识条件语句if语句switch语句 函数定义有名函数和匿名函数可选参数剩余参数箭头函数 类1. 类的定义2.…

华为认证 | 这门HCIA认证正式发布!

华为认证云计算工程师HCIA-Cloud Computing V5.5&#xff08;中文版&#xff09;自2023年9月28日起&#xff0c;正式在中国区发布。 01 发布概述 基于“平台生态”战略&#xff0c;围绕“云-管-端”协同的新ICT技术架构&#xff0c;华为公司打造了覆盖ICT领域的认证体系&#…