使用Intent在活动之间穿梭

1.在com.example.activitytest中创建第二个活动SecondActivity:

/**
* 第二个活动
*/
public class SecondActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
}
}

创建完成后会自动生成second_layout.xml,这里我们进行修改如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 2"
/>
</LinearLayout>

这时AndroidManifest.xml已经帮我们注册了活动:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activitytest"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".FirstActivity"
android:label="this is first">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"></activity>
</application> </manifest>

2.使用Intent启动活动

Intent是Android中各组件进行交互的一种重要方式,它不仅可以指明当前组件想要执行的动作,还可以在不同组件之间传递数据.

Intent大致可以分为两种:显示Intent和隐式Intent

一.显式Intent

Intent中有多个构造函数的重载,其中一个Intent(Context packageContext,Class<?> cls),这个构造函数第一个参数是启动活动的上下文,第二个启动活动的目标.

            //启动第二个活动
public void onClick(View v) {
//FirstActivity.this 指上下文 SecondActivity.class指活动目标
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intent);
}

二.隐式Intent

通过<activity>标签下配置<intent-filter>的内容,可以指定当前活动能够响应的action和category,

打开AndroidManifest.xml,添加如下代码:

 <activity android:name=".SecondActivity">
<intent-filter>
<action android:name="com.example.activitytest.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

修改FirstActivity中按钮的点击事件:

 //隐式使用Intent
public void onClick(View v) {
Intent intent = new Intent("com.example.activitytest.ACTION_START");
startActivity(intent);
}

可以选择添加多个category:

//隐式使用Intent
public void onClick(View v) {
Intent intent = new Intent("com.example.activitytest.ACTION_START");
intent.addCategory("com.example.activitytest.MY_CATEGORY");
startActivity(intent);
}

打开AndroidManifest.xml,添加如下代码:

<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="com.example.activitytest.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.example.activitytest.MY_CATEGORY" />
</intent-filter>
</activity>

3.Intent的其他使用方法

跳转第三方链接

//跳转第三方链接
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.baidu.com"));
startActivity(intent);
}

调用拨号功能

//调用拨号功能
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:10086"));
startActivity(intent);
}

使用Intent在活动之间穿梭的更多相关文章

  1. 使用Intent在活动之间穿梭(《第一行代码》读书笔记)

    以下全是个人理解//瞎扯 其实活动理解理解起来就像一个个函数 那么Intent就是调用函数和参数传递 可以有无参,仅仅是调用 Intent intent = new Intent(A.this, B. ...

  2. 从0系统学Android-2.3使用 Intent 在 Activity 之间穿梭

    2.3 使用 Intent 在 Activity 之间穿梭 在上一节中我们已经学会了如何创建一个 Activity 了.对于一个应用程序来说,肯定不可能只有一个 Activity.下面就来学习多个 A ...

  3. Android: Intent实现活动之间的交互

    Intent的作用:是Android中各个组件直接交互的一种重要方式,且利用Intent可以启动Activity.Service以及Broadcast Receiver. Intent的创建:显示和隐 ...

  4. intent 活动之间穿梭

    1.从当前activity,跳转到当前应用程序的activity Intent intent = new Intent(MainActivity.this, Intent2Activity.class ...

  5. (三)使用Intent在活动中穿梭:显式和隐式Intent

    一.显式Intent @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstan ...

  6. ###Intent的使用(活动中穿梭)

    让活动切换有两种方式 显示意图和隐式意图 显示意图:只能在本应用中穿梭: 隐式意图:可以调用其他应用程序的活动,包括系统应用,但是需要配置清单文件 显式Intent 1) 创建一个新的活动 2) 确定 ...

  7. 我的Android第五章:通过Intent实现活动与活动之间的交互

    Intent在活动的操作 作用: Itent是Android程序中各个组件直接交换的一个重要方式可以指定当前组件要执行任务同时也可以给各个组件直接进行数据交互              同时Inten ...

  8. intent,实现两个活动之间数据的传递

    一.Intent 可以启动一个活动,也可以在启动活动的时候传递数据.intent中提供了putExtra()方法,它可以把我们想要传递的数据暂存在intent中,启动了另一个活动后,通过getInte ...

  9. 在活动之间切换(显式Intent)

    实验名称:在活动之间切换 实验现象:通过点击主活动的按钮进入下一个界面 使用技术:显式Intent 步骤: 1.创建一个项目,加载布局.添加一个button 2.新建一个活动. 3.修改按钮的点击事件 ...

随机推荐

  1. dlib安装教程(for linux)

    https://blog.csdn.net/LoHiauFung/article/details/78454905 https://www.linuxidc.com/Linux/2017-11/148 ...

  2. Date日期操作

    获取年月日时分秒: package com.util; import java.text.DateFormat; import java.util.Calendar; import java.util ...

  3. delphi 动态加载dll

    引入文件 DLL比较复杂时,可以为它的声明专门创建一个引入单元,这会使该DLL变得更加容易维护和查看.引入单元的格式如下: unit MyDllImport; {Import unit for MyD ...

  4. Sublime Text 3 格式化HTML CSS JS 代码

    一,首先通过ctrl+shift+p 要等一会就会出现插件安装界面 二,在插件安装输入框,输入:HTML-CSS-JS Prettify  并安装该插件 三,如果没有装nodejs, 下载nodejs ...

  5. PYQT5实现 关闭 提示弹框

    当关闭窗口时,要实现如下功能: def closeEvent(self, event): reply = QtWidgets.QMessageBox.question(self, '警告', '退出后 ...

  6. 用 python 修改文件中指定的行数

    #! /bin/python filename='setup.ini' lines=[] with open(filename,'r') as f: lines=f.readlines() lines ...

  7. 600. Non-negative Integers without Consecutive Ones

    Given a positive integer n, find the number of non-negative integers less than or equal to n, whose ...

  8. webservice gsoap 小记

    参考 http://www.cs.fsu.edu/~engelen/soap.html 1. web service client application > wsdl2h -s -o MyHe ...

  9. Python(socketserver并发聊天)

    day27 一个server与多个client聊天. server.py import socketserver class MyServer(socketserver.BaseRequestHand ...

  10. 三,memcached服务的两种访问方式

    memcached有两种访问方式,分别是使用telnet访问和使用php访问. 1,使用telnet访问memcacehd 在命令提示行输入, (1)连接memcached指令:telnet 127. ...