Android笔记(三) 使得Activity之间可以跳转---Intent
什么是Intent
一个APP肯定不单单由一个Activity构成,我们在使用过程中,经常需要在多个Activity中跳转,Android中Intent可以帮我们来完成在各个Activity中跳转的功能。
Intent翻译为中文为“意图,目的”,在Android中提供了Intent机制来协助应用之间的交互和通讯,Intent负责对应用中一次操作的动作、动作涉及的数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将Intent传递给调用的组件,并完成组件的调用。
Intent不仅可以用于程序之间,也可以用于程序内部。
Intent分为隐式Intent和显式Intent。
显式Intent
明确指出了目标组件名称的Intent,被称之为显式Intent。显式Intent直接用组件的名称定义目标组件,这种方式很直接。但是开发人员往往并不清楚别的应用程序的组件名称,因此,显示Intent更多用于应用程序内部传递消息。
在显式Intent消息中,决定目标组件的唯一要素就是组件名称,因此,如果你的Intent中已经明确定义了目标组件的名称,那么你就完全不用再定义其他Intent内容。
1. 新建一个工程LearnIntent
2. MainActivity.java
package cn.lixyz.learnintent; import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到button组件设置点击事件
findViewById(R.id.btnStartMyAty).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//显式方式声明Intent,并启动
startActivity(new Intent(MainActivity.this,MyAty.class));
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId(); //noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} return super.onOptionsItemSelected(item);
}
}
3. MyAty.java
package cn.lixyz.learnintent; import android.app.Activity;
import android.os.Bundle; /**
* Created by LGB on 2015/8/6.
*/
public class MyAty extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myaty);
}
}
4. acitivity_main.xml
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start MyAty"
android:id="@+id/btnStartMyAty"/> </RelativeLayout>
5. myaty.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"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button" />
</LinearLayout>
6. AndroidManiFest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.lixyz.learnintent" > <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <activity
android:name=".MyAty" >
</activity>
</application> </manifest>
运行结果,点击“Start MYATY”,会转跳到MyAty中去


隐式Intent
隐式Intent在创建时候并不指定要启动的activity,而是由Android系统帮助应用程序寻找与Intent请求意图最匹配的组件。
Android系统寻找与Intent请求意图最匹配的组件具体的选择方法是:Android将Intent的请求内容与一个叫做intent-filter的过滤器比较,intent-filter中包含系统中所有可能的待选组件。如果Intent-Filter中的某一组件隐式匹配Intent的请求,那么Android就选择该组件作为隐式Intent的目标组件。如果有多个组件满足该Intent请求,那么则会弹出提示,
1. 新建一个工程LearnIntent
2. MainActivity.java
package cn.lixyz.learnintent; import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("asdkfjalskd");
startActivity(intent);
}
});
}
}
3. SecondActivity.java
import android.app.Activity;
import android.os.Bundle; /**
* Created by LGB on 2015/8/6.
*/
public class SecondActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
}
}
4. acitivity_main.xml
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击跳转到另一个页面"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
5. second_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二个页面"
android:id="@+id/second_view"/> </LinearLayout>
6. AndroidManiFest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.lixyz.learnintent" > <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="asdkfjalskd" /> <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application> </manifest>
运行程序,点击按钮,成功转跳到第二个activity中


在上面的代码中,MainActivity.java中Intent请求内容为“asdkfjalskd”,这时候会在AndroidManiFest中的Intent-Filter寻找最匹配的activity,恰好SecondActivity满足,所以会跳转到SecondActivity上面去
上面说了,那个请求字符串可以是任意字符串,假如说两个activity的Intent-Filter中的action相同呢?
7.ThirdActivity.java
import android.app.Activity;
import android.os.Bundle; /**
* Created by LGB on 2015/8/6.
*/
public class ThirdActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third_layout);
}
}
8.third_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第三个页面"
android:id="@+id/third_view" />
</LinearLayout>
9. AndroidManiFest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.lixyz.learnintent" > <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="asdkfjalskd" /> <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity> <activity
android:name=".ThirdActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="asdkfjalskd" /> <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application> </manifest>
运行我们会发现,当点击按钮时候,会提示用户选择跳转到哪个activity

选择之后,跳转成功
在实际开发中,我们约定俗成的使用“包名.intent.action.类名”来代替那个字符串,以免发生混乱
Android笔记(三) 使得Activity之间可以跳转---Intent的更多相关文章
- Android 笔记-Fragment 与 Activity之间传递数据
Fragment 与 Activity之间传递数据有两种方法.一种是使用setArgument,一种是使用接口回调.以下先学习第一种方法. (1)使用setArgument方法: 为了便于理解,我在这 ...
- 实现android activity之间的跳转
android程序一般不会只有一个activity,会碰到activity之间的跳转.以下是使用Intent做应用程序内部的activity做跳转.比如,应用程序第一个activity是: 点击“下一 ...
- Android activity之间的跳转和数据传递
1.Activity之间的跳转 并且 传递数据 A Activity进行的操作 Intent intent = new Intent(context, B.class); intent.putExtr ...
- 杂记之activity之间的跳转
代码结构图 manifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xml ...
- Android 写一个Activity之间来回跳转的全局工具类(主要是想实现代码的复用)
废话不多说了,直接上代码,相信大家都能看得懂的. 一.主要工具类 package com.yw.chat.utils; import android.app.Activity; import andr ...
- Android 数据传递(一) Activity之间的数据传递
bundle Google Bundle类说明 Bundle类是一个key-value对.Activity之间的数据通信可以通过bundle类来实现数据的存储.即将数据放入bundle里面,将Bund ...
- Android中Fragment与Activity之间的交互(两种实现方式)
(未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...
- 【转】Android进阶2之Activity之间数据交流(onActivityResult的用法)----不错
原文网址:http://blog.csdn.net/sjf0115/article/details/7387467 主要功能: 在一个主界面(主Activity)上能连接往许多不同子功能模块(子Act ...
- ActivityJump+ActivityManager【Activity之间的跳转和Activity任务栈管理】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 封装Activity跳转的方法以及实现Activity任务栈管理. 效果图 代码分析 ActivityJump:封装Activi ...
随机推荐
- 【prometheus 抓取源】
配置prometheus从prometheus爬取数据 prometheus提供了下面这个端口来让其他prometheus来抓取(scrape)自己的时序数据: http://prometheus_i ...
- Git出现There is no tracking information for the current branch提示的解决办法
参考:https://blog.csdn.net/sinat_36246371/article/details/79738782 在执行git pull的时候,提示当前branch没有跟踪信息: Th ...
- 对 Watchbog Botnet 渗透过程和 Payload 的分析
漏洞利用 CVE-2018-1000861 https://jenkins.io/security/advisory/2018-12-05/ Watchbog在做什么? Watchbog僵尸网络为其所 ...
- 2019年Java面试题基础系列228道(4)
1.Java 中能创建 volatile 数组吗? 能,Java 中可以创建 volatile 类型数组,不过只是一个指向数组的引用,而不是整个数组.我的意思是,如果改变引用指向的数组,将会受到 vo ...
- linux中信号的API详解实例
/************************************************************************* > File Name: signal.c ...
- Linux下安装redis以及常用命令
https://blog.csdn.net/zgf19930504/article/details/51850594 安装: 1.获取redis资源 wget http://download.redi ...
- springboot的mapper.xml在src下问题
在pom.xml里面的build标签加上resources说明 <resources> <!-- mapper.xml文件在java目录下 --> <resource&g ...
- nohup保证程序后台运行
前言 我们运行某些命令的时候,它会默认在前台执行.如果要进行其他操作,则需要先停掉此程序.然后就蛋疼了. 解决 碰到这种情况,我们可以使用"nohup"命令和"&am ...
- LeetCode 589. N叉树的前序遍历(N-ary Tree Preorder Traversal)
589. N叉树的前序遍历 589. N-ary Tree Preorder Traversal LeetCode589. N-ary Tree Preorder Traversal 题目描述 给定一 ...
- 关于/etc/rc.local
/etc/rc.d/rc.local 用于添加开机启动命令 /etc/rc.local是/etc/rc.d/rc.local的软连接 简单来说 开机自启的