一.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
<?xml version="1.0" enCoding="utf-8"?>
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"
androID:orIEntation="vertical"
androID:layout_wIDth="fill_parent"
androID:layout_height="fill_parent"
>
<TextVIEw
androID:layout_wIDth="fill_parent"
androID:layout_height="wrap_content"
androID:text="@string/hello"
/>
<button
androID:text="测试Action属性"
androID:ID="@+ID/getBtn"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
/>
</linearLayout>
strings.xml
<?xml version="1.0" enCoding="utf-8"?>
<resources>
<string name="hello">测试Action属性</string>
<string name="app_name">IntentActionDemo</string>
</resources>
MainActivity.java
public class MainActivity extends Activity {
private button getBtn;
@OverrIDe
public voID onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.main);
getBtn=(button)findVIEwByID(R.ID.getBtn);
getBtn.setonClickListener(new OnClickListener() {
@OverrIDe
public voID onClick(VIEw v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);// 设置Intent Action属性
intent.setType("vnd.androID.cursor.item/phone");// 设置Intent Type 属性
//主要是获取通讯录的内容
startActivity(intent); // 启动Activity
}
});
}
}
效果图:
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
<?xml version="1.0" enCoding="utf-8"?>
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"
androID:orIEntation="vertical" androID:layout_wIDth="fill_parent"
androID:layout_height="fill_parent"
>
<TextVIEw
androID:layout_wIDth="fill_parent"
androID:layout_height="wrap_content"
androID:text="测试Intent category"
/>
<button
androID:ID="@+ID/button1"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
androID:text="转到Home界面"
/>
</linearLayout>
strings.xml
<?xml version="1.0" enCoding="utf-8"?>
<resources>
<string name="hello">Hello World, MainActivity!</string>
<string name="app_name">IntentcategoryDemo</string>
</resources>
MainActivity.java
package com.androID.category.activity;
import androID.app.Activity;
import androID.content.Intent;
import androID.os.Bundle;
import androID.vIEw.VIEw;
import androID.vIEw.VIEw.OnClickListener;
import androID.Widget.button;
public class MainActivity extends Activity {
private button btn;
@OverrIDe
public voID onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.main);
btn = (button)findVIEwByID(R.ID.button1);
btn.setonClickListener(new OnClickListener() {
@OverrIDe
public voID onClick(VIEw v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);// 添加Action属性
intent.addcategory(Intent.category_HOME);// 添加category属性
startActivity(intent);// 启动Activity
}
});
}
}
效果图:
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
<?xml version="1.0" enCoding="utf-8"?>
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"
androID:orIEntation="vertical"
androID:layout_wIDth="fill_parent"
androID:layout_height="fill_parent"
>
<TextVIEw
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
androID:text="请输入用户名"
/>
<EditText
androID:ID="@+ID/EditText1"
androID:layout_wIDth="fill_parent"
androID:layout_height="wrap_content"
/>
<button
androID:ID="@+ID/button1"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
androID:text="测试Extras属性"
/>
</linearLayout>
second.xml
<?xml version="1.0" enCoding="utf-8"?>
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"
androID:orIEntation="vertical"
androID:layout_wIDth="fill_parent"
androID:layout_height="fill_parent"
>
<TextVIEw
androID:ID="@+ID/TextVIEw1"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
/>
</linearLayout>
strings.xml
<?xml version="1.0" enCoding="utf-8"?>
<resources>
<string name="hello">Hello World, FirstActivity!</string>
<string name="app_name">IntentExtrasDemo</string>
</resources>
FirstActivity.java
public class FirstActivity extends Activity {
private button btn;
private EditText etx;
@OverrIDe
public voID onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.first);
btn = (button)findVIEwByID(R.ID.button1);
etx = (EditText)findVIEwByID(R.ID.EditText1);
btn.setonClickListener(new OnClickListener() {
@OverrIDe
public voID onClick(VIEw v) {
Intent intent = new Intent();
//设置Intent的class属性,跳转到SecondActivity
intent.setClass(FirstActivity.this, SecondActivity.class);
//为intent添加额外的信息
intent.putExtra("usename", etx.getText().toString());
//启动Activity
startActivity(intent);
}
});
}
}
SecondActivity.java
public class SecondActivity extends Activity {
private TextVIEw tv;
@OverrIDe
public voID onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置当前的Activity的界面布局
setContentVIEw(R.layout.second);
//获得Intent
Intent intent = this.getIntent();
tv = (TextVIEw)findVIEwByID(R.ID.TextVIEw1);
//从Intent获得额外信息,设置为TextVIEw的文本
tv.setText(intent.getStringExtra("usename"));
}
}
注意:在添加第二个Activity SecondActivity的时候,要在AndroIDManifest.xml里面添加上SecondActivity,具体如下,即是在15行</activity>的后面添加上16~18行的代码。如果不这样做,就会在模拟器上出现错误。
<?xml version="1.0" enCoding="utf-8"?>
<manifest xmlns:androID="http://schemas.androID.com/apk/res/androID"
package="com.androID.extras.activity"
androID:versionCode="1"
androID:versionname="1.0">
<uses-sdk androID:minSdkVersion="10" />
<application androID:icon="@drawable/icon" androID:label="@string/app_name">
<activity androID:name=".FirstActivity"
androID:label="@string/app_name">
<intent-filter>
<action androID:name="androID.intent.action.MAIN" />
<category androID:name="androID.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity androID:name=".SecondActivity"
androID:label="@string/app_name">
</activity>
</application>
</manifest>
效果图:
本文出自 “IT的点点滴滴” 博客:http://liangruijun.blog.51cto.com/3061169/634411
--------------------------------------------------------------------
PS: 欢迎关注公众号"Devin说",会不定期更新Java相关技术知识。
--------------------------------------------------------------------
转载于:https://www.cnblogs.com/devinzhang/archive/2012/01/17/2324778.HTML
总结以上是内存溢出为你收集整理的Intent的简介以及属性详解全部内容,希望文章能够帮你解决Intent的简介以及属性详解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)