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 ...
随机推荐
- 报错:java.lang.AbstractMethodError: nl.techop.kafka.KafkaHttpMetricsReporter.logger()Lcom/typesafe/scalalogging/Logger;
报错背景: CDH启动kafka的时候出现报错情况,并且报错的节点挂掉. 报错现象: Exiting Kafka due to fatal exception java.lang.AbstractMe ...
- Java extract amplitude array from recorded wave
转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-extract-amplitude-array-from.html Extra ...
- CentOS "libc.so.6: version 'GLIBC_2.14' not found"解决方法,同理'GLIBC_2.15' not found"
出现"libc.so.6: version 'GLIBC_2.14' not found"问题,是由于glibc版本过低,升级glibc即可. 由于CentOS系统RPM源目前gl ...
- ACS712电流传感器应用
1. 原理图 其中第7脚输出的是电压值,那么电压值和测量的电流什么关系?看下图,有3个量程,我用的是20A电流的,100mv电压对应1A电流 看下图,不同的温度会有影响,不过区别不大 最后计算的公式是 ...
- console.log()和alert()的区别
一直都是知道console.log()和alert()是有区别的,但是具体有什么区别就不清楚了,后来在权威指南里注意到了说alert()具有侵入性才来查一查两者的具体区别. 查询到的区别: alert ...
- string中的erase()函数
erase()是对string类型的字符串进行删除元素操作的函数 1.erase(int index) 删除下标从index开始直到字符串结尾的元素 1 string s = "123215 ...
- springboot的propteis的基本配置参考
其中mybatis.cfg.xml文件可以不加,这个文件最主要是开启mybatis的二级缓存:
- MongoDB 空间定位(点) 与 距离检索
转自: http://blog.csdn.net/flamingsky007/article/details/39208837 基于 MongoDB 2.6 GeoJSON 格式 { "ty ...
- PAT(B) 1052 卖个萌(Java:0分 待解决,C:20分)
题目链接:1052 卖个萌 (20 point(s)) 题目描述 萌萌哒表情符号通常由"手"."眼"."口"三个主要部分组成.简单起见,我们 ...
- WUSTOJ 1320: 饭卡(Java)动态规划-背包
题目链接: