Android 开发系列 3 Intent
2015-07-02 10:19
447 查看
Intent作用
Android使用Intent用来在应用间通讯Intent启动组件
组件名称 | 方法名称 |
---|---|
Activity | startActvity( ) startActivity( ) |
Service | startService( ) bindService( ) |
Broadcasts | sendBroadcasts() sendOrderedBroadcasts() sendStickyBroadcasts( ) |
Intent的组件
Action属性
指Intent要完成的动作,是一个字符串常量列 | 目标组件 | 动作 |
---|---|---|
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. |
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/
相关文章推荐
- Android studio第一次使用配置(三)gradle项目构建
- Android 开发系列 2 Activity
- android:屏幕自适应
- Activity与Fragment数据传递之Activity之间获取数据 分类: Android 2015-07-02 10:13 10人阅读 评论(0) 收藏
- Android开发学习资源之(四)
- Android studio第一次使用配置(二)新建工程并安装Genymotion模拟器
- Android studio 使用Menu(菜单)
- Android Studio插件之Android Layout ID Converter教程
- Android百度地图导航部分报JNI相关错误
- Android Studio 快捷键
- Android studio开发环境下载安装
- Android-BroadcastReceiver
- android自定义换行居中CenterTextView
- 【转】【Android】HAL分析
- Android开发好习惯之二
- 保留所有Android crash信息
- Android-ContentProvider使用
- Android开发学习资源之(三)
- android下内容提供者
- Android studio第一次使用配置