Intent及其属性详解

一.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常量如下表所示。

Action常量 对应字符串 简单说明
ACTION_MAIN android.intent.action.MAIN 应用程序入口
ACTION_VIEW android.intent.action.VIEW 显示指定数据
ACTION_ATTACH_DATA android.intent.action.ATTACH_DATA 指定某块数据将被附加到其它地方
ACTION_EDIT android.intent.action.EDIT 编辑指定数据
ACTION_PICK android.intent.action.PICK 从列表中选择某项并返回所选的数据
ACTION_CHOOSER android.intent.action.CHOOSER 显示一个Activity选择器
ACTION_GET_CONTENT android.intent.action.GET_CONTENT 让用户选择数据,并返回所选数据
ACTION_DIAL android.intent.action.DIAL 显示拨号面板
ACTION_CALL android.intent.action.CALL 直接向指定用户打电话
ACTION_SEND android.intent.action.SEND 向其他人发送数据
ACTION_SENDTO android.intent.action.SENDTO 向其他人发送消息
ACTION_ANSWER android.intent.action.ANSWER 应答电话
ACTION_INSERT android.intent.action.INSERT 插入数据
ACTION_DELETE android.intent.action.DELETE 删除数据
ACTION_RUN android.intent.action.RUN 运行维护
ACTION_SYNC android.intent.action.SYNC 执行数据同步
ACTION_PICK_ACTIVITY android.intent.action.PICK_ACTIVITY 用于选择Activity
ACTION_SEARCH android.intent.action.SEARCH 执行搜索
ACTION_WEB_SEARCH android.intent.action.WEB_SEARCH 执行Web搜索
ACTION_FACTORY_TEST android.intent.action.FACTORY_TEST 工厂测试的入口点

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

  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的信息。

Category 常量对应字符串 简单说明
CATEGORY_DEFAULT android.intent.category.DEFAULT 默认的Category
CATEGORY_BROWSABLE android.intent.category.BROWSABLE 指定该Activity能被浏览器安全调用
CATEGORY_TAB android.intent.category.TAB 指定Activity作为TabActivity的Tab页
CATEGORY_LAUNCHER android.intent.category.LAUNCHER Activity显示顶级程序列表中
CATEGORY_INFO android.intent.category.INFO 用于提供包信息
CATEGORY_HOME android.intent.category.HOME 设置该Activity随系统启动而运行
CATEGORY_PREFERENCE android.intent.category.PREFERENCE 该Activity是参数面板
CATEGORY_TEST android.intent.category.TEST 该Activity是一个测试
CATEGORY_CAR_DOCK android.intent.category.CAR_DOCK 指定手机被插入汽车底座(硬件)时运行该Activity
CATEGORY_DESK_DOCK android.intent.category.DESK_DOCK 指定手机被插入桌面底座(硬件)时运行该Activity
CATEGORY_CAR_MODE android.intent.category.CAR_MODE 设置该Activity可在车载环境下使用

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

  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的Component属性

Intent的Component属性指定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>

效果图:

时间: 2024-08-27 06:46:52

Intent及其属性详解的相关文章

EditText属性详解

关于EditText属性详解很详细的记录,转过来收着 1.EditText输入的文字为密码形式的设置 (1)通过.xml里设置: 把该EditText设为:android:password="true" // 以”.”形式显示文本 (2)在代码里设置: 通过设置EditText的setTransformationMethod()方法来实现隐藏密码或这显示密码. editText.setTransformationMethod(PasswordTransformationMethod.g

css动画-animation各个属性详解(转)

CSS3的animation很容易就能实现各种酷炫的动画,虽然看到别人的成果图会觉得很难,但是如果掌握好各种动画属性,做好酷炫吊炸天的动画都不在话下,好,切入正题. 一.动画属性: 动画属性包括:①animation-name,②animation-duration,③animation-timing-function, 以下是各属性详解: 1.animation-name:指定要绑定到选择器的关键帧的名称. 2.animation-duration:定义动画完成一个周期需要多少秒或毫秒 3.a

jQuery Event.which 属性详解

jQuery Event.which 属性详解 which属性用于返回触发当前事件时按下的键盘按键或鼠标按钮. 对于键盘和鼠标事件,该属性用于确定你按下的是哪一个键盘按键或鼠标按钮. which属性对DOM原生的event.keyCode和event.charCode进行了标准化. 适用的事件类型主要有键盘事件:keypress.keydown.keyup,以及鼠标事件:mouseup.mousedown. 该属性属于jQuery的Event对象(实例). 语法 jQuery 1.1.3 新增该

WPF依赖属性详解

WPF依赖属性详解 WPF 依赖属性 英文译为 Dependency Properties,是WPF引入的一种新类型的属性,在WPF中有着极为广泛的应用,在WPF中对于WPF Dependency Properties 的使用贯穿样式的使用,数据绑定,动画等等,在刚刚接触Dependency Properties的时候可能觉得有些奇怪,但是,当你了解他要解决的问题的时候,你可能就不觉得奇怪了.Dependency Properties第一个要解决的问题就是控件的属性共享问题,由于大部分的WPF控

Hibernate fetch属性详解

主要参考 :http://4045060.blog.51cto.com/4035060/1088025 一.hibernate抓取策略(单端代理的批量抓取fetch=select(默认)/join) 1)保持默认,同fetch="select" <many-to-one name="group" column="group_id" fetch="select" /> 另外发送一条select语句抓取当前对象关联实体

MWPhotoBrowser 属性详解 和代理解释

--------0.MWPhoto简单属性解释---------------- MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3629/3339128908_7aecabc34b.jpg"]]; photo.caption = @"在将photo添加到数组中时,可以在这里设置标题名字"; photo = [MWPhotophot

Android textAppearance的属性设置及TextView属性详解

textAppearance的属性设置 android:textAppearance="?android:attr/textAppearanceSmall" android:textAppearance="?android:attr/textAppearanceMedium" android:textAppearance="?android:attr/textAppearanceLarge" TextView属性详解 android:autoLi

Shape 各种属性详解

本文来自:http://blog.csdn.net/brokge/article/details/9713041 简介: 作用:XML中定义的几何形状 位置:res/drawable/文件的名称.xml 使用的方法: Java代码中:R.drawable.文件的名称 XML中:Android:background="@drawable/文件的名称" 属性: <shape>形状 Android:shape=["rectangle" | "oval

HTML中META属性详解 转载自 hero_213的博客

HTML中META属性详解 meta是html语言head区的一个辅助性标签.几乎所有的网页里,我们可以看到类似下面这段的html代码: <head> <meta   http-equiv= "content-Type "   content= "text/html;   charset=gb2312 "> </head>         也许你认为这些代码可有可无.其实如果你能够用好meta标签,会给你带来意想不到的效果,例如加