Intent寻找目标组件的两种方式:

  • 显式Intent:通过指定Intent组件名称来实现的,它一般用在知道目标组件名称的前提下,一般是在相同的应用程序内部实现的。
  • 隐式Intent:通过Intent Filter来实现的,它一般用在没有明确指出目标组件名称的前提下,一般是用于在不同应用程序之间。

一.显式Intent

一般情况下,一个Android应用程序中需要多个屏幕,即是多个Activity类,并且在这些Activity之间进行切换通过Intent机制来实现的。在同一个应用程序中切换Activity时,我们通常都知道要启动的Activity具体是哪一个,因此常用显式的Intent来实现的。

下面的例子是在同一应用程序中MainActivity启动SecondActivity,下面的代码中,主要是为“转到SecondActivity”按钮添加了OnClickListener,使得按钮被点击时执行onClick()方法,onClick()方法中则利用了Intent机制,来启动SecondActivity,关键的代码是22~25行。

main.xml

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello1"
  11. />
  12. <Button
  13. android:id="@+id/btn"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="转到SecondActivity"
  17. />
  18. </LinearLayout>

second.xml

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello2"
  11. />
  12. <Button
  13. android:id="@+id/secondBtn"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="返回"
  17. />
  18. </LinearLayout>

MainActivity.java

  1. package com.android.test.activity;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. publicclass MainActivity extends Activity {
  9. private Button btn;
  10. @Override
  11. publicvoid onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. btn = (Button)findViewById(R.id.btn);
  15. //响应按钮btn事件
  16. btn.setOnClickListener(new OnClickListener() {
  17. @Override
  18. publicvoid onClick(View v) {
  19. //显示方式声明Intent,直接启动SecondActivity
  20. Intent it = new Intent(MainActivity.this,SecondActivity.class);
  21. //启动Activity
  22. startActivity(it);
  23. }
  24. });
  25. }
  26. }

SecondActivity.java

  1. package com.android.test.activity;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. publicclass SecondActivity extends Activity {
  9. private Button secondBtn;
  10. @Override
  11. protectedvoid onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.second);
  14. secondBtn=(Button)findViewById(R.id.secondBtn);
  15. //响应按钮secondBtn事件
  16. secondBtn.setOnClickListener(new OnClickListener() {
  17. @Override
  18. publicvoid onClick(View v) {
  19. //显示方式声明Intent,直接启动MainActivity
  20. Intent intent = new Intent(SecondActivity.this,MainActivity.class);
  21. //启动Activity
  22. startActivity(intent);
  23. }
  24. });
  25. }
  26. }

AndroidManifest.xml清单文件,16~18行为SecondActivity在清单文件里的声明

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.android.test.activity"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <uses-sdkandroid:minSdkVersion="10"/>
  7. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
  8. <activityandroid:name=".MainActivity"
  9. android:label="@string/app_name">
  10. <intent-filter>
  11. <actionandroid:name="android.intent.action.MAIN"/>
  12. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  13. </intent-filter>
  14. </activity>
  15. <activityandroid:name=".SecondActivity"
  16. android:label="@string/app_name">
  17. </activity>
  18. </application>
  19. </manifest>

效果图:

二.隐式Intent

下面是同一应用程序中的Activity切换的例子,需要AndroidManifest.xml中增加Activity的声明,并设置对应的Intent Filter和Action,才能被Android的应用程序框架所匹配。

MainActivity.java

  1. package com.android.change.activity;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. publicclass MainActivity extends Activity {
  9. private Button btn;
  10. @Override
  11. publicvoid onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. btn = (Button) findViewById(R.id.btn);
  15. // 响应按钮btn事件
  16. btn.setOnClickListener(new OnClickListener() {
  17. @Override
  18. publicvoid onClick(View v) {
  19. // 实例化Intent
  20. Intent it = new Intent();
  21. //设置Intent的Action属性
  22. it.setAction("com.android.activity.MY_ACTION");
  23. // 启动Activity
  24. startActivity(it);
  25. }
  26. });
  27. }
  28. }

SecondActivity.java

  1. package com.android.change.activity;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. publicclass SecondActivity extends Activity {
  5. @Override
  6. protectedvoid onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.second);
  9. }
  10. }

main.xml

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. />
  11. <Button
  12. android:id="@+id/btn"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="转到SecondActivity"
  16. />
  17. </LinearLayout>

seond.xml

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/second"
  11. />
  12. </LinearLayout>

AndroidManifest.xml文件的18,19行修改了Intent Filter,这样SecondActivity才能够接收到MainActivity发送的Intent。因为在MainActivity的Intent发送的动作为"com.android.activity.MY_ACTION",而在18行里,SecondActivity设置的Action也为"com.android.activity.MY_ACTION",这样就能进行匹配。

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.android.change.activity"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <uses-sdkandroid:minSdkVersion="10"/>
  7. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
  8. <activityandroid:name=".MainActivity"
  9. android:label="@string/app_name">
  10. <intent-filter>
  11. <actionandroid:name="android.intent.action.MAIN"/>
  12. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  13. </intent-filter>
  14. </activity>
  15. <activityandroid:name=".SecondActivity">
  16. <intent-filter>
  17. <actionandroid:name = "com.android.activity.MY_ACTION"/>
  18. <categoryandroid:name = "android.intent.category.DEFAULT"/>
  19. </intent-filter>
  20. </activity>
  21. </application>
  22. </manifest>

效果图:

 

对于显示Intent,Android不需要再去做解析,因为目标组件很明确。Android需要解析的是隐式Intent,通过解析,将Intent映射给可以处理该Intent的Activity,Service等。Intent的解析机制主要是通过查找已经注册在AndroidManifest.xml中的所有IntentFilter以及其中定义的Intent,最终找到匹配的Intent。

Android开发学习之浅谈显示Intent和隐式Intent的更多相关文章

  1. Android -- 两个activity界面的切换, 显示Intent 和 隐式Intent,putExtra传递数据

    1. 两个Activity之间可以通过Intent切换, 包括显示Intent 和 隐式Intent. 实例代码 MainActivity.java public class MainActivity ...

  2. 显式Intent 和隐式 Intent 的区别

    显式 Intent : 在知道目标组件名称的前提下,去调用Intent.setComponent().Intent.setClassName()或Intent.setClass()方法或者在new I ...

  3. Android开发学习笔记:浅谈显示Intent和隐式Intent

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://liangruijun.blog.51cto.com/3061169/655132 ...

  4. 在Android中Intent的概念及应用(一)——显示Intent和隐式Intent

    Intent寻找目标组件的两种方式: 显式Intent:通过指定Intent组件名称来实现的,它一般用在知道目标组件名称的前提下,一般是在相同的应用程序内部实现的. 隐式Intent:通过Intent ...

  5. 【Android】6.0 添加Menu菜单组件、Intent启动活动、显式Intent、隐式Intent

    1.0 在helloworld项目基础上创建活动SecondActivity: 2.0 其中main.xml: <?xml version="1.0" encoding=&q ...

  6. 2018.7.9 Android—显式Intent和隐式Intent的区别

    1:都是用来在一个activity中启动另外一个activity 2:显示Intent直接指明要启动activity的定义,即activity.class:隐式intent通过在androidmani ...

  7. 显式Intent和隐式Intent

    http://blog.csdn.net/qs_csu/article/details/7995966 对于明确指出了目标组件名称的Intent,我们称之为“显式Intent”. 对于没有明确指出目标 ...

  8. android开发学习---layout布局、显示单位和如何进行单元测试

    一.五大布局(layout) android中的用五大布局:LinearLayout (线性布局).AbsoluteLayout(绝对布局).RelativeLayout(相对布局).TableLay ...

  9. 从0系统学Android-2.4隐式Intent

    本系列文章,参考<第一行代码>,作为个人笔记 更多内容:更多精品文章分类 使用隐式 Intent 相对于显示 Intent ,隐式 Intent 比较含蓄.这种方式不明确指出我们想要启动哪 ...

随机推荐

  1. @Java VisualVM分析堆转储文件

    测试程序 public class HeapOOM { private static int i = 0; static class OOMObject { } public static void ...

  2. QtWebKit

    WekKit官网:http://www.webkit.org/ QtWebKit官网及安装:http://trac.webkit.org/wiki/QtWebKit#GettingInvolved Q ...

  3. 10 款基于 jQuery 的切换效果插件推荐

    本文整理了 10 款非常好用的 jQuery 切换效果插件,包括平滑切换和重叠动画等,这些插件可以实现不同元素之间的动态切换. 1. InnerFade 这是一个基于 jQuery 的小插件,可以实现 ...

  4. scala 学习笔记十三 特质(转载)

    转载地址:https://blog.csdn.net/dwb1015/article/details/51761510 1,介绍 Scala和java一样不允许类从多个超类继承:从多个超类继承可能会导 ...

  5. 关于PHP写的投票网站之刷票终结版

    告诉大家一个坏消息,还是有刷票的行为,有图有真相: 倒数第三项是时间轴,倒数第二项是IP,倒数第一项是邮箱,你们要刷票,拦都拦不住呀呀呀呀呀呀!!!!! 看看这些时间的间隔,看看这些IP的地址,哎,再 ...

  6. 交叉编译git

    git依赖openssl.zlib. 首先编译openssl ./Configure linux-armv4 shared 修改Makefile,CC.RANLIB.MAKEDEPPROG为对应的交叉 ...

  7. 【转】gcc选项

    http://zodiac1111.github.io/blog/config-gcc-warning/

  8. Linux随笔(安装ftp,安装jdk,安装 tomcat,安装redis,安装MySQL,安装svn)

    su: authentication failure 解决办法:sudo passwd root  更改密码即可 确认虚拟机用到的联网方式是桥接模式,不然Windows是ping不通Linux的,确保 ...

  9. 【Nodejs】nimble或async并不能保证程序串行执行,回调是回避不了的坑

    先看一段例程: //------------------------------- // 用于创建目录 //------------------------------- function creat ...

  10. 从Ubuntu12.04LTS到Foreda19再到Foreda8

    装Ubuntu的初衷是以为它能识别我的PCI无线网卡,但装了两遍没有做到. 昨天在Ubuntu装jdk7,其过程与正常Linux安装jdk差别不小,有点背离的意思.另外VI的用法也和正常Unix/Li ...