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. HBase入门基础教程之单机模式与伪分布式模式安装(转)

    原文链接:HBase入门基础教程 在本篇文章中,我们将介绍Hbase的单机模式安装与伪分布式的安装方式,以及通过浏览器查看Hbase的用户界面.搭建HBase伪分布式环境的前提是我们已经搭建好了Had ...

  2. OpenCV学习(24) 直方图(1)

    直方图是对数据的统计,并将统计结果分布于一系列预定义的槽中.这里的数据不仅仅指的是灰度值,它可以是任何能有效描述图像特征的数据,比如图像梯度等等. 假设有一个矩阵包含一张图像的信息 (灰度值 0-25 ...

  3. 3D屏保:排色榜

    3D屏保:排色榜 排色榜,是一个针对图形学中的色彩进行排序的DEMO,这里的色是色彩的意思,看成别的点进来的同学请自觉面壁.该DEMO可以按RGB,GBR,BRG,灰度值四种方式进行排序.排序算法为冒 ...

  4. C 图像处理 颜色相关宏定义

    很多年前整理的,像素处理的宏定义,包括r8g8b8到r5g6b5之间的相互转化,浮点数像素与整数值之间的相互转化,像素值的插值.取反等处理.具体没什么好说的,宏定义的代码还是很容易看的.这套东西对搞图 ...

  5. 2.6 《硬啃设计模式》第8章 复制不是很难 - 原型模式(Prototype Pattern)

    案例: 某即时战略游戏,你训练出来各种很强的战士. 为了增加游戏的可玩性,增加了一种复制魔法.实施该魔法,可以复制任意的战士. 你会怎样考虑这个设计? 在继续阅读之前,请先认真思考并写出你的设计,这样 ...

  6. [leetcode]Reorder List @ Python

    原题地址:http://oj.leetcode.com/problems/reorder-list/ 题意: Given a singly linked list L: L0→L1→…→Ln-1→Ln ...

  7. Best Time to Buy and Sell Stock leetcode java

    题目: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...

  8. ASP.NET 仿腾讯微博提示“还能输入*个字符”的实现

    textbox如果设置TextMode="MultiLine"则 它的MaxLength设置的值就无效:为了能达到像腾讯微薄.新浪微薄那样的提示的效果(腾讯和新浪微薄文本框用到的应 ...

  9. Immediately-Invoked Puzzler

    The Poplar Puzzle-makers weren’t too impressed. They barely noticed your simple and beautiful array ...

  10. Android 之数据传递小结

    Android开发中,在不同模块(如Activity)间经常会有各种各样的数据需要相互传递,常用的的有五种传递方式.它们各有利弊,有各自的应用场景.下面分别介绍一下: 1. Intent对象传递简单数 ...