您的位置:首页 > 移动开发 > Android开发

Android 开发系列 3 Intent

2015-07-02 10:19 447 查看

Intent作用

Android使用Intent用来在应用间通讯

Intent启动组件

组件名称方法名称
ActivitystartActvity( )
startActivity( )
ServicestartService( )
bindService( )
BroadcastssendBroadcasts()
sendOrderedBroadcasts()
sendStickyBroadcasts( )

Intent的组件

Action属性

指Intent要完成的动作,是一个字符串常量

目标组件动作
ACTION_CALLactivityInitiate a phone call.
ACTION_EDITactivityDisplay data for the user to edit.
ACTION_MAINactivityStart up as the initial activity of a task, with no data input and no returned output.
ACTION_SYNCactivitySynchronize data on a server with data on the mobile device.
ACTION_BATTERY_LOWbroadcast receiverA warning that the battery is low.
ACTION_HEADSET_PLUGbroadcast receiverA headset has been plugged into the device, or unplugged from it.
ACTION_SCREEN_ONbroadcast receiverThe screen has been turned on.
ACTION_TIMEZONE_CHANGEDbroadcast receiverThe setting for the time zone has changed.
测试代码

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

package com.android.action.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 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
}
});
}
}


Data属性

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

Category属性

Intent中的Category属性是一个执行动作Action的附加信息。比如:CATEGORY_HOME则表示放回到Home界面.

Type

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

Compent属性

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

Extra属性

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

本文内容参考:

http://liangruijun.blog.51cto.com/3061169/634411/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: