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

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. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns: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. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns: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.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9.  
  10. public class MainActivity extends Activity {
  11. private Button btn;
  12. @Override
  13. public void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.main);
  16.  
  17. btn = (Button)findViewById(R.id.btn);
  18. //响应按钮btn事件
  19. btn.setOnClickListener(new OnClickListener() {
  20. @Override
  21. public void onClick(View v) {
  22. //显示方式声明Intent,直接启动SecondActivity
  23. Intent it = new Intent(MainActivity.this,SecondActivity.class);
  24. //启动Activity
  25. startActivity(it);
  26. }
  27. });
  28. }
  29. }

SecondActivity.java

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

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

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

效果图:

二.隐式Intent

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

MainActivity.java

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

SecondActivity.java

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

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns: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. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns: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. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.android.change.activity"
  4. android:versionCode=""
  5. android:versionName="1.0">
  6. <uses-sdk android:minSdkVersion="" />
  7.  
  8. <application android:icon="@drawable/icon" android:label="@string/app_name">
  9. <activity android:name=".MainActivity"
  10. android:label="@string/app_name">
  11. <intent-filter>
  12. <action android:name="android.intent.action.MAIN" />
  13. <category android:name="android.intent.category.LAUNCHER" />
  14. </intent-filter>
  15. </activity>
  16. <activity android:name=".SecondActivity" >
  17. <intent-filter>
  18. <action android:name = "com.android.activity.MY_ACTION" />
  19. <category android:name = "android.intent.category.DEFAULT" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </manifest>

效果图:

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

本文出自 “IT的点点滴滴” 博客,请务必保留此出处http://liangruijun.blog.51cto.com/3061169/655132

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

  1. Android开发学习之浅谈显示Intent和隐式Intent

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

  2. android开发学习笔记000

    使用书籍:<疯狂android讲义>——李刚著,2011年7月出版 虽然现在已2014,可我挑来跳去,还是以这本书开始我的android之旅吧. “疯狂源自梦想,技术成就辉煌.” 让我这个 ...

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

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

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

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

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

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

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

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

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

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

  8. 显式Intent和隐式Intent

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

  9. Android开发学习笔记:Intent的简介以及属性的详解【转】

    一.Intent的介绍 Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述 ...

随机推荐

  1. CentOS7中升级Docker版本

    参考:http://blog.csdn.net/liumiaocn/article/details/52130852

  2. IT在线学习网站总结

    以下是我自己做软件过程中发现的一些不错的IT学习网站,个人感觉比较受用,故总结出来以供IT爱好者一起学习: www.maiziedu.com  麦子学院 www.jikexueyuan.com 极客学 ...

  3. JS学习知我见(常用建站代码)

    <!doctype html><html><head><meta charset="utf-8"><meta name=&qu ...

  4. JAVA初学(1):值类型和引用类型的区别

    JAVA值类型和引用类型的区别(转)                                                          [定义] 引用类型表示你操作的数据是同一个,也就 ...

  5. win10 剪贴板 拒绝访问 Cannot open clipboard

    win10 Cannot open clipboard:拒绝访问. 在RAD IDE代码编辑器中,双击选中的文本,会自动复制到剪贴板里,导致的问题是 从 A处复制文本 到B处双击选中,粘贴的时候,是B ...

  6. 在Android中自定义捕获Application全局异常,可以替换掉系统的强制退出对话框(很有参考价值与实用价值)

    转载自: http://blog.csdn.net/jdsjlzx/article/details/7606423

  7. group_concat函数使用

    t1表 语句: select type,group_concat(name) from t1 group by type 结果

  8. PostgreSQL系列一:PostgreSQL简介与安装

    一.PostgreSQL简介     1.1 PostgreSQL概述             PostgreSQL数据库是目前功能最强大的开源数据库,支持丰富的数据类型(如JSON和JSONB类型. ...

  9. 在线api地址

    J2SE1.7英文api地址: http://download.oracle.com/javase/7/docs/api/J2SE1.6英文api地址:  http://download.oracle ...

  10. CloudStack系统部署系列教程-KVM

    之前培训时获得的资料,以防丢失,故发布此以做备份.