Intent代表了Android应用的启动“意图”,Android应用将会根据Intent来启动指定组件,至于到底启动哪个组件,取决于Intent的各个属性。

一、显式的Intent

明确指定了要启动的组件的Intent我们称为显式的Intent

例如:

  1. package com.example.testintent;
  2. import android.app.Activity;
  3. import android.content.ComponentName;
  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. public class MainActivity extends Activity {
  10. Button button;
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. button = (Button)findViewById(R.id.button1);
  16. button.setOnClickListener(new OnClickListener() {
  17. @Override
  18. public void onClick(View arg0) {
  19. ComponentName comp = new ComponentName(MainActivity.this, SecondActivity.class);
  20. Intent intent = new Intent();
  21. intent.setComponent(comp);
  22. startActivity(intent);
  23. }
  24. });
  25. }
  26. }

注意在manifest文件里注册SecondActivity

Intent的Componet属性需要接收一个ComponentName对象,ComponetName对象其实是一个指定包和要启动的Activity路径的类,有如下几个构造

  1. componentName(String pkg, String cls)
  2. componentName(Context pkg, String cls)
  3. componentName(Context pkg, Class<?> cls)

componentName(Parcel in)

除了这个属性之外Intent还包含了如下三个方法:

  1. setClass(Context packageContext, Class<?> cls )
  2. setClassName(Context packageContext, String className)
  3. setClassName(String packageName, String className )

上面的代码可以简化为如下代码:

  1. Intent intent = new Intent(MainActivity.this, SecondActivity.class);
  2. startActivity(intent);

 

二、隐式的Intent

没有明确指定要启动的组件的Intent我们称为隐式的Intent

Intent除了上面的Componet属性外还有Action、Category属性

Action代表Intent所要完成的一个抽象动作,而Category则是动作附加的类别信息。

例如:

  1. package com.example.testintent;
  2. import android.app.Activity;
  3. import android.content.ComponentName;
  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. public class MainActivity extends Activity {
  10. Button button;
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. button = (Button)findViewById(R.id.button1);
  16. button.setOnClickListener(new OnClickListener() {
  17. @Override
  18. public void onClick(View arg0) {
  19. Intent intent = new Intent();
  20. intent.setAction("com.example.intent.action.TEST_ACTION");
  21. startActivity(intent);
  22. }
  23. });
  24. }
  25. }

manifest文件中配置

  1. <activity
  2. android:name=".SecondActivity">
  3. <intent-filter >
  4. <action android:name="com.example.intent.action.TEST_ACTION"/>
  5. <category android:name="android.intent.category.DEFAULT"/>
  6. </intent-filter>
  7. </activity>

一个Intent只能指定一个Action属性,可以包含多个Category属性,当程序创建时,默认启动category为DEFAULT的组件。

接下来我们来看看Category属性的用法

  1. public void onClick(View arg0) {
  2. Intent intent = new Intent();
  3. intent.setAction("com.example.intent.action.TEST_ACTION");
  4. intent.addCategory("android.intent.category.TEST_CATEGERY");
  5. startActivity(intent);
  6. }

manifest文件中配置

  1. <activity
  2. android:name=".SecondActivity">
  3. <intent-filter >
  4. <action android:name="com.example.intent.action.TEST_ACTION"/>
  5. <category android:name="android.intent.category.DEFAULT"/>
  6. <category android:name="android.intent.category.TEST_CATEGERY"/>
  7. </intent-filter>
  8. </activity>

可以看出其实是根据Action和Category两个属性共同决定启动哪个Activity的,Category可以有多个,只要满足其中的一个即可。

实际上Intent不仅可以启动我们定义的Activity,也可以启动系统和其他应用的Activity

这里仅列出部分更多关于Action和Category属性请参阅:http://developer.android.com/reference/android/content/Intent.html

三、一个获取通讯录的实例:

  1. package com.example.testintent;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.database.Cursor;
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.provider.ContactsContract;
  8. import android.provider.ContactsContract.Contacts;
  9. import android.support.v4.content.CursorLoader;
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.view.View.OnClickListener;
  13. import android.widget.Button;
  14. public class MainActivity extends Activity {
  15. private static final String TAG = "MainActivity";
  16. Button button;
  17. final int PICK_CONTACT = 0;
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22. button = (Button)findViewById(R.id.button1);
  23. button.setOnClickListener(new OnClickListener() {
  24. @Override
  25. public void onClick(View arg0) {
  26. Intent intent = new Intent();
  27. intent.setAction(Intent.ACTION_GET_CONTENT);
  28. intent.setType("vnd.android.cursor.item/phone");
  29. startActivityForResult(intent, PICK_CONTACT);
  30. }
  31. });
  32. }
  33. @Override
  34. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  35. super.onActivityResult(requestCode, resultCode, data);
  36. switch (requestCode) {
  37. case PICK_CONTACT:
  38. if(resultCode == Activity.RESULT_OK){
  39. //获取返回数据
  40. Uri contactData = data.getData();
  41. CursorLoader cursorLoader = new CursorLoader(this, contactData, null, null, null, null);
  42. Cursor cursor = cursorLoader.loadInBackground();
  43. if(cursor.moveToFirst()){
  44. String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
  45. String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
  46. Log.i(TAG, "姓名:" + name);
  47. //根据联系人查看详细信息
  48. Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
  49. ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
  50. if(phones.moveToFirst()){
  51. System.out.println("进来了");
  52. String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
  53. Log.i(TAG, "电话:" + phoneNumber);
  54. }
  55. //关闭游标
  56. phones.close();
  57. }
  58. //关闭游标
  59. cursor.close();
  60. }
  61. break;
  62. default:
  63. break;
  64. }
  65. }
  66. }

Android之旅-Intent与Intent Filter[上]的更多相关文章

  1. Android菜鸟的成长笔记(8)——Intent与Intent Filter(上)

    原文:[置顶] Android菜鸟的成长笔记(8)——Intent与Intent Filter(上) Intent代表了Android应用的启动“意图”,Android应用将会根据Intent来启动指 ...

  2. Android开发之旅: Intents和Intent Filters(理论部分)

    引言 大部分移动设备平台上的应用程序都运行在他们自己的沙盒中.他们彼此之间互相隔离,并且严格限制应用程序与硬件和原始组件之间的交互. 我们知道交流是多么的重要,作为一个孤岛没有交流的东西,一定毫无意义 ...

  3. Android菜鸟的成长笔记(9)——Intent与Intent Filter(下)

    原文:[置顶] Android菜鸟的成长笔记(9)——Intent与Intent Filter(下) 接着上一篇的内容,下面我们再来看看Intent的Data与Type属性. 一.Data属性与Typ ...

  4. Android基础Activity篇——Intent返回数据给上一个活动

    1.如果活动B要将数据返回给活动A,那么需要以下三步: 1.1在活动A中使用startActivityForResult()方法启动活动B. 1.2在活动B中使用setResult()方法传回Iten ...

  5. 我的Android 4 学习系列之Intent 和 Broadcast Reciever

    目录 Intent 简介 使用隐式和显式Intent启动Activity.子Acitivity和Service 使用Linkify 使用Broadcast Intent 广播事件 使用 Pending ...

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

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

  7. Android入门(二)Activity-Toast、Intent

    原文链接:http://www.orlion.ga/427/ 一.隐藏activity的标题 在activity的java代码的onCreate()方法中入requestWindowFeature(W ...

  8. Android开发-API指南-常用Intent

    Common Intents 英文原文:http://developer.android.com/guide/components/intents-common.html 采集(更新)日期:2014- ...

  9. Android开发-API指南-Intent和Intent过滤器

    Intents and Intent Filters 英文原文:http://developer.android.com/guide/components/intents-filters.html 采 ...

随机推荐

  1. Jquery——几个注意的小知识

    event.stopPropagation() 停止事件冒泡 event.preventDefault()//组织默认行为(例如错误的时候,阻止按钮提交) event.type获取事件类型 event ...

  2. ZH奶酪:利用CSS将checkbox选项放大

    在Hybrid App开发过程中,html中默认的checkbox选项在手机屏幕上显得比较小,app不能像web page那样放大缩小,所以要通过CSS将checkbox选项放大: 例如HTML代码: ...

  3. Python+H5py实现将SVHN样本库转换为FasterRcnn训练样本

    一.上代码 import os import h5py svhnPath = 'D:\\Project\\AIProject\\SVHNClassifier\\data' def loadSvhn(p ...

  4. 单页面SPA应用路由原理 history hash

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  5. 【Linux】shell编程案例

    一.随机生成字符文件名 1.需求描述: 使用for循环在/usr/test目录下创建10个html文件,其中每个文件需要包含10个随机小写字母加固定字符串test,案例名称如下: 以下为示例: 2.代 ...

  6. mysql取年、月、日、时间

    select id, phone,time,year(time),month(time), DAY(time),TIME(time) from user where phone='xxxxxx'  # ...

  7. 使用Cookie进行会话管理

    javaweb学习总结(十一)——使用Cookie进行会话管理 一.会话的概念 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. ...

  8. win10更新后无法远程,报 credssp加密oracle修正

    答案都在图里,看不清就浏览器放大观看 打开开始菜单,搜索“编辑组策略”  进入

  9. HTML5音频audio属性

    来源:https://segmentfault.com/a/1190000008932943?utm_source=tuicool&utm_medium=referral audio 的控制函 ...

  10. Android自定义控件进阶-打造Android自定义的下拉列表框控件

    技术:Android+java   概述 Android中的有个原生的下拉列表控件Spinner,但是这个控件有时候不符合我们自己的要求, 比如有时候我们需要类似windows 或者web网页中常见的 ...