[Android] Intent启动activity方式

Android Activity的启动流程(Android-10) 深入认识startActivity(Intent) 阅读详情

在Android系统中,应用程序一般都有多个Activity,Intent可以实现不同Activity之间的切换和数据传递,Intent启动Activity方式:显式启动和隐式启动。

显式启动:
按名称(完全限定类名)指定要启动的组件。 通常,会在自己的应用中使用显式Intent来启动组件,这是因为知道要启动的Activity或服务的类名。
创建显式Intent启动Activity或服务时,系统将立即启动Intent对象中指定的应用组件。

隐式Intent :
不指定特定的组件,而是声明要执行的常规操作,从而允许其他应用中的组件来处理它。

  • Android系统根据Intent的动作和数据来决定启动哪一个Activity
  • 在隐式启动时,Intent中只包含需要执行的动作和所包含的数据,而无需指明具体启动哪一个Activity,选择权由Android系统和最终用户来决定

1.运行图

界面:

在这里插入图片描述

显式启动:

在这里插入图片描述
在这里插入图片描述

隐式启动:

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

打开子活动:

在这里插入图片描述
在这里插入图片描述

确认按钮:

在这里插入图片描述

取消按钮:

在这里插入图片描述

2.代码

(1)activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp" />

    <Button
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:onClick="explit"
        android:text="显式启动"
        android:textSize="28sp" />

    <Button
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:onClick="implit"
        android:text="隐式启动"
        android:textSize="28sp" />

    <Button
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:onClick="implit1"
        android:text="打开子活动方式1"
        android:textSize="28sp" />

    <Button
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:onClick="implit2"
        android:text="打开子活动方式2"
        android:textSize="28sp" />


</LinearLayout>

(2)MainActivtiy.java

package com.example.activity;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    private EditText myinfo = null;
    private ActivityResultLauncher<Intent> activityResultLauncher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myinfo = findViewById(R.id.info);
        activityResultLauncher = registerForActivityResult
                (new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
                    @Override
                    public void onActivityResult(ActivityResult result) {
                        if (result.getData() != null) {//没有请求码,单一处理
                            if (result.getResultCode() == RESULT_OK) {
                                String temp = result.getData().getStringExtra("reva");
                                myinfo.setText(temp);
                            } else {
                                myinfo.setText("对方没有返回值");
                            }
                        }
                    }
                });

    }

    public void explit(View view) {
//        Intent intent=new Intent(MainActivity.this,SecondActivity.class);
//        intent.putExtra("value",myinfo.getText().toString());
//        startActivity(intent);
        SecondActivity.activityStart(MainActivity.this, myinfo.getText().toString());
    }

    public void implit(View view) {
        Intent intent = new Intent(Intent.ACTION_VIEW,
                Uri.parse(myinfo.getText().toString()));
        startActivity(intent);
    }

    public void implit1(View view) {
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        intent.putExtra("value", myinfo.getText().toString());
        startActivityForResult(intent, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    String temp = data.getStringExtra("reva");
                    myinfo.setText(temp);
                } else {
                    myinfo.setText("对方没有返回值");
                }
                break;
        }
    }

    public void implit2(View view) {
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        intent.putExtra("value", myinfo.getText().toString());
        activityResultLauncher.launch(intent);
    }

}

(3)activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SecondActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="接受的值:"
            android:textSize="30sp"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/receiveinfo"
            android:textSize="30sp"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="要返回的值:"
            android:textSize="30sp"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/returninfo"
            android:textSize="30sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="确定"
            android:onClick="ok"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="取消"
            android:onClick="cancel"
            />
    </LinearLayout>

</LinearLayout>

(4)SecondActivity.java

package com.example.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {
    private TextView myview=null;
    private EditText myedit=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        myview=findViewById(R.id.receiveinfo);
        myedit=findViewById(R.id.returninfo);

        Intent intent=getIntent();
        String temp=intent.getStringExtra("value");
        myview.setText(temp);
    }

    public static void activityStart(Context context,String data){
        Intent intent=new Intent(context,SecondActivity.class);
        intent.putExtra("value",data);
        context.startActivity(intent);
    }

    public void ok(View view) {
        String temp=myedit.getText().toString();
        Intent intent=new Intent();
        intent.putExtra("reva",temp);
        setResult(RESULT_OK,intent);//设置返回值
        finish();
    }

    public void cancel(View view) {
        Intent intent=new Intent();
        setResult(RESULT_CANCELED,intent);
        finish();
    }
}

增加一个新的活动SecondAcitivity,也能打开网页,则在AndroidManifest.xml如下图设置:
在这里插入图片描述

【达内课程】Android中Intent使用详解 Intent 意图 Intent在Android系统的作用: 1、激活系统组件,例如startActivity(Intent),startService(Intent) 2、在系统组件之间传递数据 栗子 两个Activity之间数据传递,从MainActivity跳转到SecondActivity,同时传递数据 activity_main &lt;LinearLayout xm... 阅读详情

相关推荐

Android开发之Activity

Android开发学习笔记。文中记录了Android开发中的三个常用组件:Activity(活动)、Broadcast(广播)、Service(服务),以及如何在组件之间传递消息数据。

u013054484的博客 3265

使用intent来启动activity

Intent最常见的用途是绑定应用程序组件,并在应用程序

Crazy Android 6460

基于CY7C68013A芯片的USB2.0设计

基于FPGA的USB2.0读写控制项目简介CY7C68013A芯片介绍CY7C68013A芯片读写时序控制CY7C68013A芯片读写代码USB2.0的测试结果结束语 项目简介 我们前面已经写过了USB3.0的FPGA实现,所用到的芯片是FX600Q芯片,这次我们讲解USB2.0的FPGA代码的编写。我们用到的芯片是Cypress厂家的Cy7C68013A,该款芯片在市场上的应用率极广,我们这里不...

zhangningning1996的博客 1万+

使用Intent启动Activity

Intent的最常用的用途是绑定应用程序组件。Intent用来在应用程序的Activity间启动、停止和传输。 这一章节中关于启动新的Activity的提示,也同样适用于Service。第8章可以了解到关于启动(和创建)Service的细节。 为了打开应用程序中不同的画面(Activity),调用startActivity,传入一个Intent,如下面的片段所示...

de14141983的专栏 612

coze+飞书多维表格,批量分析小红书笔记,关键还能获取图片(附保姆级教程)

通过coze工作流,一键同步小红书账号和笔记链接到飞书多维表格,效率飞升~

qq_24403067的博客 4175

AnolisOS网卡驱动源码编译与安装实战指南

本文提供了一份详尽的AnolisOS网卡驱动源码编译与安装实战指南。针对系统自带通用驱动不兼容导致网络无法使用的问题,文章从环境准备、源码获取、编译安装到新旧驱动替换与持久化配置,一步步解析了如何通过源码编译安装官方驱动,解决硬件兼容性问题,并确保驱动在系统重启后自动生效。

weixin_28358083的博客 304

Intent详解(一)----显、隐式开启Activity

Intent开启Activity 1.Intent的作用 Intent是一个可以消息传递对象,可以通过它来进行组件之间的信息传递。Intent主要有以下三个作用: l  开启一个activity l  开启一个service l  发送广播消息 本篇主要介绍Intent如何开启一个Activity。 Intent可以启动Activity,启动的方式有显式启动和隐式启动两种。显

zys_1997的博客 2万+

冰河木马实验(V8.4)

简述: 百科词条 冰河木马开发于1999年,跟灰鸽子类似,在设计之初,开发者的本意是编写一个功能强大的远程控制软件。但一经推出,就依靠其强大的功能成为了黑客们发动入侵的工具,并结束了国外木马一统天下的局面,跟后来的灰鸽子等等成为国产木马的标志和代名词。 冰河的服务器端程序为G-server.exe,客户端程序为G-client.exe,默认连接端口为7626。其中攻击者使用G-client.e...

野原新之助 1万+

Android开发工程师文集-Activity生命周期,启动方式,Intent相关介绍,Activity详细讲解

前言大家好,我是 Vic,今天给大家带来Android开发工程师文集-Activity生命周期,启动方式,Intent相关介绍,Activity详细讲解的概述,希望你们喜欢Activity是什么作为一个Activity,就是一个界面,当我们在手机上打开一个APP时,你看到的页面就是基于Activity生成的。那么你再点击一个按钮跳转到另一个界面时,就是又一个Activity界面,由Activity...

程序员哆啦A梦,蓝胖子 514

2.STM32F407ZGT6 学习笔记-移植 FreeRTOS

1. 在工程下建议FreeRTOS文件夹 2. 在FreeRTOS文件夹下建立 src 和 port 文件夹 (src用于存放freertos源码,port文件夹存放内存和接口相关的文件) 3. 复制freertos/source文件夹下所有c文件复制到刚刚建立的freertos/src目录下 4. 复制FreeRTOSv9.0.0\FreeRTOS\Source\portablex下的MemMang和RVDS文件夹复制到刚刚建立的port问价夹下 5. 将FreeRTOSv9.0.0\FreeRTOS

Car12 1233

基于情感分析+聚类分析+LDA主题分析对服装产品类的消费者评论分析

本研究旨在通过对服装产品类的消费者评论进行综合分析,为企业提供更全面、更深入的市场洞察和决策支持。同时,本研究的方法和结果也可以为其他领域的消费者评论分析提供借鉴和参考。

m0_64336780的博客 2万+

android-Intent启动activity的几种方式-隐式Intent

1.ios中通知的使用: a 对象注册,并关连消息 [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(viewBack)name:@"viewBack"object:nil]; 参数介绍:          addObserver: 观察者,即在什么地方接收通知;

daiyelang的专栏 1万+

Oracle EBS Tax 四国实现详解:印度、巴西、美国、阿根廷

印度:务必启用India Localization模块,不要尝试用标准EBTax模拟GST逻辑 巴西:必须配置NF-e接口,建议聘请本地税务顾问设置CFOP和NCM代码映射 美国:跨州业务强烈建议购买Vertex或Avalara服务,自建税率表维护成本极高 阿根廷:注意1:1:1架构限制,提前规划Legal Entity和OU结构

金牌架构师 261

安卓开发-Activity

元数据时一种描述其他数据的数据,它相当于固定活动的参数信息(一般用于开发的软件需要使用别的软件的功能:获取目标软件的activity,meta里的value对应的是一个类似于验证码的东西,只有填写正确的value才能使用软件的功能) 直接在AndroidManifest.xml对应的Activity节点内部添加。传递复杂数据对象时,需要对象时可序列化的,实现Serializable或者Parcelable。元数据不仅能传递简单的字符串参数,还能传递更复杂的资源数据(支付宝的快捷方式菜单) 元数据的。

weixin_74118846的博客 885

YOLOv8交通监控全方案:精准车辆计数+违章(闯红灯/压线)识别,拿来就落地!

作为深耕计算机视觉落地的开发者,我见过太多交通监控的“伪智能”——传统系统要么车辆计数不准(漏计/重复计数),要么违章识别滞后(靠人工回看录像),甚至连“闯红灯、压线”这类常见违章都无法实时判定,完全满足不了实际交通管理需求。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 646

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

Android之显示意图启动Activity

前言 显示意图启动Activity很简单,基本上做过Android的人都知道,怎么去启动,这里就做个简单的总 结!总体分为两种,一种是直接在创建Intent时,就传入Context对象与要启动的Activity对象的 Class类;另一种是使用到ComponentName这个类。通过观察源码发现,其实第一种方式也是使用的 ComponentName类,只是将它封装起来。 直接创...

yangshuangyue的博客 1289

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Android基础(四)——Activity的两种启动方式、Intent初探

一、启动Activity 一个activity跳转到另外一个activity中的最简单的方式就是使用下面的Activity方法: public void startActivity(Intent intent); 可能会有人认为startActivity(...)方法是一个类方法,启动activity就是针对Activity子类调用该方法。实际并非如此,activity调用startActi

左大侠的博客 2173

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

【Android页面启动Intent】从一个APP启动另一个APP的activity的方式

Android Bug分析系列:第三方平台安装app启动后,home键回到桌面后点击app启动时会再次启动入口类bug的原因剖析 https://www.cnblogs.com/net168/p/5722752.html

chezabo6116的博客 364

Android Activity 的四种启动模式 lunchMode 和 Intent.setFlags();singleTask的两种启动方式。...

Android Activity 的四种启动模式 lunchMode 和 Intent.setFlags(); 一.Android Activity 四种启动模式 lunchMode 在Manifest 配置文件中的属性如下: (1) android:launchMode="standard" (2)android:launchMode="singleTop" (3)androi...

dieji7118的博客 274

android intent singletask,Android Activity 的四種啟動模式 lunchMode 和 Intent.setFlags();singleTask的兩種啟動方式。...

Android Activity 的四種啟動模式 lunchMode 和 Intent.setFlags();一.Android Activity 四種啟動模式 lunchMode 在Manifest 配置文件中的屬性如下:(1) android:launchMode="standard"(2)android:launchMode="singleTop"(3)android:launchMode=...

weixin_29980355的博客 262

【安卓开发-3】Activity的四种启动模式、Intent的显式和隐式使用方式

1、Activity的启动模式有四种:standard、singleTop、singleTask、singleInstance。默认是standard。这个配置是AndroidManifest.xml里面对某一个Activity进行设置。 我们利用下面的代码,把当前的任务栈和实验的Activity的地址都显示出来,方便说明。public class MainActivity extends App

小小的客栈 大大的江湖 1680

android 隐式启动,Activity 显式启动与隐式启动 应用

一、Activity 显式启动 (代码示例):public class MyActivity extends Activity {Button buttonStart;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.lay...

weixin_30420799的博客 1091

Android开发之Intent的使用

前言:对于初入安卓开发的小白来说,感觉Intent的两种方法没啥区别,都可以启动一个Activity,是的都可以启动一个Activity,但是两个启动的Activity的侧重点不同,今天我们就来详细看一下Intent的两种启动方式以及其他需要注意的东西!

等待着冬天的风的博客 753

android中activity启动模式中对象地址$1是什么意思,Activity启动另一个Activity的方式总结...

下面两种方式均采用这个menifests.xml布局,且下面两种方式属于显性方式{即通过使用(MainActivity.this, xxxx.class)}; 一,用intent构造器进行启动: 二,用Component属性: 下面的方法是隐性方法即没有明确的指明(MainActivity.this, xxxx.class);三,用Action和Category属性:采用这个布局: 采用的Java...

weixin_33548183的博客 391
上一篇: [Android] 选项卡组件TabHost
下一篇: [Android] 实验3.1 组件通信与广播消息之Intent的使用
冰落風絡
博客等级 码龄5年 28粉丝 16原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值