Android开发学习笔记:Intent的简介以及属性的详解

2011-08-08 17:20:48

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

一.Intent的介绍

Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。Intent不仅可用于应用程序之间,也可用于应用程序内部的Activity/Service之间的交互。因此,可以将Intent理解为不同组件之间通信的“媒介”专门提供组件互相调用的相关信息。

二.Inten启动组件的方法

Intent可以启动一个Activity,也可以启动一个Service,还可以发起一个广播Broadcasts。具体方法如下:

组件名称

方法名称

Activity

startActvity( )

startActivity( )

Service

startService( )

bindService( )

Broadcasts

sendBroadcasts( )

sendOrderedBroadcasts( )

sendStickyBroadcasts( )

三.Intent的属性

Intent有以下几个属性:

动作(Action),数据(Data),分类(Category),类型(Type),组件(Compent)以及扩展信(Extra)。其中最常用的是Action属性和Data属性。

1.Intent的Action属性

Action是指Intent要完成的动作,是一个字符串常量。SDK中定义了一些标准的Action常量如下表所示。

Constant

Target component

Action

ACTION_CALL

activity

Initiate a phone call.

ACTION_EDIT

activity

Display data for the user to edit.

ACTION_MAIN

activity

Start up as the initial activity of a task, with no data input and no returned output.

ACTION_SYNC

activity

Synchronize data on a server with data on the mobile device.

ACTION_BATTERY_LOW

broadcast receiver

A warning that the battery is low.

ACTION_HEADSET_PLUG

broadcast receiver

A headset has been plugged into the device, or unplugged from it.

ACTION_SCREEN_ON

broadcast receiver

The screen has been turned on.

ACTION_TIMEZONE_CHANGED

broadcast receiver

The setting for the time zone has changed.

下面是一个测试Action常量的例子:

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/hello"
  11. />
  12. <Button
  13. android:text="测试Action属性"
  14. android:id="@+id/getBtn"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. />
  18. </LinearLayout>

strings.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="hello">测试Action属性</string>
  4. <string name="app_name">IntentActionDemo</string>
  5. </resources>

MainActivity.java

  1. package com.android.action.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. public class MainActivity extends Activity {
  9. private Button getBtn;
  10. @Override
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. getBtn=(Button)findViewById(R.id.getBtn);
  15. getBtn.setOnClickListener(new OnClickListener() {
  16. @Override
  17. public void onClick(View v) {
  18. Intent intent = new Intent();
  19. intent.setAction(Intent.ACTION_GET_CONTENT);// 设置Intent Action属性
  20. intent.setType("vnd.android.cursor.item/phone");// 设置Intent Type 属性
  21. //主要是获取通讯录的内容
  22. startActivity(intent); // 启动Activity
  23. }
  24. });
  25. }
  26. }

效果图:

2.Intent的Data属性

Intent的Data属性是执行动作的URI和MIME类型,不同的Action有不同的Data数据指定。比如:ACTION_EDIT Action应该和要编辑的文档URI Data匹配,ACTION_VIEW应用应该和要显示的URI匹配。

3.Intent的Category属性

Intent中的Category属性是一个执行动作Action的附加信息。比如:CATEGORY_HOME则表示放回到Home界面,ALTERNATIVE_CATEGORY表示当前的Intent是一系列的可选动作中的一个。下表是SDK文档中关于Category的信息。

Constant

Meaning

CATEGORY_BROWSABLE

The target activity can be safely invoked by the browser to display data referenced by a link — for example, an image or an e-mail message.

CATEGORY_GADGET

The activity can be embedded inside of another activity that hosts gadgets.

CATEGORY_HOME

The activity displays the home screen, the first screen the user sees when the device is turned on or when the HOME key is pressed.

CATEGORY_LAUNCHER

The activity can be the initial activity of a task and is listed in the top-level application launcher.

CATEGORY_PREFERENCE

The target activity is a preference panel.

下面是一个回到Home界面的例子:

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" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. >
  6. <TextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="测试Intent Category"
  10. />
  11. <Button
  12. android:id="@+id/Button1"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="转到Home界面"
  16. />
  17. </LinearLayout>

strings.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="hello">Hello World, MainActivity!</string>
  4. <string name="app_name">IntentCategoryDemo</string>
  5. </resources>

MainActivity.java

  1. package com.android.category.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. public class MainActivity extends Activity {
  9. private Button btn;
  10. @Override
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. btn = (Button)findViewById(R.id.Button1);
  15. btn.setOnClickListener(new OnClickListener() {
  16. @Override
  17. public void onClick(View v) {
  18. Intent intent = new Intent();
  19. intent.setAction(Intent.ACTION_MAIN);// 添加Action属性
  20. intent.addCategory(Intent.CATEGORY_HOME);// 添加Category属性
  21. startActivity(intent);// 启动Activity
  22. }
  23. });
  24. }
  25. }

效果图:

4.Intent的Type属性

Intent的Type属性显式指定Intent的数据类型(MIME)。一般Intent的数据类型能够根据数据本身进行判定,但是通过设置这个属性,可以强制采用显式指定的类型而不再进行推导。

5.Intent的Compent属性

Intent的Compent属性指定Intent的的目标组件的类名称。通常 Android会根据Intent 中包含的其它属性的信息,比如action、data/type、category进行查找,最终找到一个与之匹配的目标组件。但是,如果 component这个属性有指定的话,将直接使用它指定的组件,而不再执行上述查找过程。指定了这个属性以后,Intent的其它所有属性都是可选的。

6.Intent的Extra属性

Intent的Extra属性是添加一些组件的附加信息。比如,如果我们要通过一个Activity来发送一个Email,就可以通过Extra属性来添加subject和body。

下面的例子在第一个Activity的EditText输入用户名,该年龄保存在Intent的Extras属性中。当单击Button时,会在第二个Activity中显示用户名。

first.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="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="请输入用户名"
  11. />
  12. <EditText
  13. android:id="@+id/EditText1"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. />
  17. <Button
  18. android:id="@+id/Button1"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:text="测试Extras属性"
  22. />
  23. </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:id="@+id/TextView1"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. />
  12. </LinearLayout>

strings.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="hello">Hello World, FirstActivity!</string>
  4. <string name="app_name">IntentExtrasDemo</string>
  5. </resources>

FirstActivity.java

  1. package com.android.extras.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. import android.widget.EditText;
  9. public class FirstActivity extends Activity {
  10. private Button btn;
  11. private EditText etx;
  12. @Override
  13. public void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.first);
  16. btn = (Button)findViewById(R.id.Button1);
  17. etx = (EditText)findViewById(R.id.EditText1);
  18. btn.setOnClickListener(new OnClickListener() {
  19. @Override
  20. public void onClick(View v) {
  21. Intent intent = new Intent();
  22. //设置Intent的class属性,跳转到SecondActivity
  23. intent.setClass(FirstActivity.this, SecondActivity.class);
  24. //为intent添加额外的信息
  25. intent.putExtra("useName", etx.getText().toString());
  26. //启动Activity
  27. startActivity(intent);
  28. }
  29. });
  30. }
  31. }

SecondActivity.java

  1. package com.android.extras.activity;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.widget.TextView;
  6. public class SecondActivity extends Activity {
  7. private TextView tv;
  8. @Override
  9. public void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. //设置当前的Activity的界面布局
  12. setContentView(R.layout.second);
  13. //获得Intent
  14. Intent intent = this.getIntent();
  15. tv = (TextView)findViewById(R.id.TextView1);
  16. //从Intent获得额外信息,设置为TextView的文本
  17. tv.setText(intent.getStringExtra("useName"));
  18. }
  19. }

注意:在添加第二个Activity SecondActivity的时候,要在AndroidManifest.xml里面添加上SecondActivity,具体如下,即是在15行</activity>的后面添加上16~18行的代码。如果不这样做,就会在模拟器上出现错误。

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

效果图:

学习笔记 intent属性的更多相关文章

  1. Android学习笔记-Intent(一)

    Intent对象在Android官方API这样描述:It is a passive data structure holding an abstract description of an opera ...

  2. Android开发学习笔记Intent 一

    Inten的概念 1.Intent是Android四大组件直接沟通的桥梁 2.Intent是一种运行时绑定(runtime binding)机制 Intent对象的属性 Itent的种类 Inten过 ...

  3. Android学习笔记Intent二

    上篇随笔大概写了了Intent学习的大纲,这篇通过代码理解下Intent的ComponentName属性的使用 ComponentName,中文意思是组件名称,通过Intent的setsetCompo ...

  4. MongoDB学习笔记~数据模型属性为集合时应该为它初始化

    回到目录 今天要说一下技术点,我们在设计mongodb的数据模型时,如果属性是数组或者集合类型,我们在模型初始化时,需要为它们初始化一下,否则在数据库里将会被存储为NULL,当被存储为NULL时,我们 ...

  5. WPF学习笔记——依赖属性(Dependency Property)

    1.什么是依赖属性 依赖属性是一种可以自己没有值,并且通过Binding从数据源获得值(依赖在别人身上)的属性,拥有依赖属性的对象被称为"依赖对象". 依赖项属性通过调用 Regi ...

  6. swift学习笔记之-属性

    //属性 import UIKit //属性(Properties)详解 /* 存储属性(Stored Properties):类.结构体中,不能在枚举中 计算属性(Computed Properti ...

  7. js学习笔记2---HTML属性操作

    1.HTML属性操作:读.写 属性名 属性值   2.属性读操作:获取.找到 a) 语法:元素.属性名 如:document.getElementById(“btn”).value; b) 字符串的连 ...

  8. vue学习笔记之属性和方法

    每个Vue都会代理其data对象里所有的属性:只有这些被代理的属性是响应的.如果在实例创建之后添加新的属性到实例上,它不会触发视图更新.例子: <script type="text/j ...

  9. CSS3学习笔记之属性值

    font-family 设置文本的字体名称. font-style 设置文本样式. 取值 normal不使用斜体. italic使用斜体. oblique使用倾斜体. inherit从父元素继承. f ...

随机推荐

  1. C#调用AForge实现摄像头录像

    1: 首先下载库文件>> 也可以去官网寻找>> 下载本教程全代码>> 输出为MP4需要用到ffmpeg相关的文件,我打包的库已经带了,去官网找的库可以在这个目录找到 ...

  2. 【SQL】- 基础知识梳理(三) - SQL连接查询

    一.引言 有时为了得到一张报表的完整数据,需要从两个或更多的表中获取结果,这时就用到了"连接查询". 二.连接查询 连接查询的定义: 数据库中的表通过键将彼此联系起来,从而获取这些 ...

  3. DotNetCore跨平台~linux上还原自主nuget包需要注意的问题

    问题的产生的背景 由于我们使用了jenkins进行部署(jenkins~集群分发功能和职责处理),而对于.net core项目来说又是跨平台的,所以对它的项目拉取,包的还原,项目的编译和项目的发布都是 ...

  4. JSP入门 el表达式

    我们已经知道el是jsp-2.0规范的一部分,tomcat-5.x版本以上都已经能够支持jsp-2.0规范,但在更低版本的tomcat和webphere,weblogic中还是无法使用这一便捷方式. ...

  5. 记一次使用快速幂与Miller-Rabin的大素数生成算法

    大家都知道RSA的加密的安全性就是能够找到一个合适的大素数,而现在判断大素数的办法有许多,比如Fermat素性测试或者Miller-Rabin素性测试,而这里我用了Miller-Rabin素性测试的算 ...

  6. Python cPickle模块

    新博客地址:http://gorthon.sinaapp.com/ 持久性就是指保持对象,甚至在多次执行同一程序之间也保持对象.通过本文,您会对 Python对象的各种持久性机制(从关系数据库到 Py ...

  7. 命令行参数处理-getopt()和getopt_long()

    在实际编程当中,自己编写代码处理命令行参数是比较麻烦且易出错的.一般我们会直接使用getopt()和getopt_long()函数,下文将介绍具体的使用方法. getopt() getopt()用于处 ...

  8. mysql error 1290 hy000:The MySQL server is running with the --skip-grant-tables option so it cannot execute this statemen' 解决方案

    如果在执行授权命令的时候报错 mysql> grant all privileges on *.* to root@"; ERROR (HY000): The MySQL server ...

  9. Color Blender---在线渐变色带生成器

       Color Blender是一个很有用的在线渐变色带生成器,它可以在两种颜色之间,自动生成过渡色,对网页设计师来说是一个不错的颜色调配工具.   Color Blender的使用方法很简单,你只 ...

  10. NodeJS 初学之安装配置环境

    [TOC] 1.环境安装 操作系统: Ubuntu 16.04.2 LTS 1.1安装nvm ryan@ryan-900X5L:~/temp$ curl https://raw.githubuserc ...