【Android】Intent简介
来源:互联网 发布:张锦洪画家淘宝网 编辑:程序博客网 时间:2024/05/22 06:10
Intent最基本的功能就是启动组件。Intent能启动android四大组件中的三种:Activity、Service和BroadcastReceiver。
Intent有如下七个重要的属性:ComponentName、Action、Category、Data、Type、Extra和Flag。
Intent分为两类:显示Intent和隐式Intent。
通过Intent的ComponentName属性去启动组件就叫显示Intent。ComponentName是android对系统组件的标识,它包含一个包名和一个类全名(都是字符串类型)。
通过Intent的属性Action、Category等去启动组件就叫隐式Intent。通过隐式Intent去启动组件需要在被启动的组件元素下配置intent-filte子元素。intent-filte可以配置Action和Category子元素,与Intent设置的Action和Category相对应。
显示Intent只能启动同一应用下的组件;隐式Intent不但能启动同一应用下的组件,也能启动其他应用下的组件(这正是隐式Intent的真正意图)。例如,要在应用中启动浏览器,就需要隐式Intent。
显示Intent会覆盖隐式Intent,也就是说如果一个Intent既配置了ComponentName,又配置了Action等,ComponentName会覆盖其他属性。
显示Intent很简单,就不多少了。下面说说隐式Intent。
隐式Intent
下面通过一个例子来说明如何通过隐式Intent来启动一个组件。
启动另一个组件activity代码:
Intent intent = new Intent();intent.setAction("com.zzj.ui.INTENTACTIVITY");intent.addCategory("com.zzj.ui.DEFAULT");intent.setDataAndType(Uri.parse("http://www.csdn.net:80/article"),"text/html");startActivity(intent);
在被启动的Activity中显示Intent信息:
package com.zzj.ui.intentdemo;import java.util.Set;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.widget.TextView;import com.zzj.ui.R;public class OtherActivity extends Activity {private TextView textView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.other_intent_activity);textView = (TextView) findViewById(R.id.textView);StringBuilder sb = new StringBuilder();Intent intent = getIntent();// 输出包名和类名sb.append("--------包名和类名--------");sb.append("\r\n");ComponentName componentName = intent.getComponent();sb.append("packageName --> ");sb.append(componentName.getPackageName());sb.append("\r\n\r\n");sb.append("className --> ");sb.append(componentName.getClassName());sb.append("\r\n\r\n");sb.append("shortClassName --> ");sb.append(componentName.getShortClassName());sb.append("\r\n\r\n");// 输出actionsb.append("--------action--------");sb.append("\r\n");sb.append(intent.getAction());sb.append("\r\n\r\n");// 输出categorysb.append("--------categories--------");sb.append("\r\n");Set<String> categories = intent.getCategories();for (String category : categories) {sb.append(category);sb.append("\r\n");}sb.append("\r\n");// 输出MimeTypesb.append("--------MimeType--------");sb.append("\r\n");sb.append("MimeType --> ");sb.append(intent.getType());sb.append("\r\n\r\n");// 输出datasb.append("--------data(uri)--------");sb.append("\r\n");Uri uri = intent.getData();sb.append("uri --> ");sb.append(uri.toString());sb.append("\r\n\r\n");sb.append("scheme --> ");sb.append(uri.getScheme());sb.append("\r\n\r\n");sb.append("host --> ");sb.append(uri.getHost());sb.append("\r\n\r\n");sb.append("port --> ");sb.append(uri.getPort());sb.append("\r\n\r\n");sb.append("path --> ");sb.append(uri.getPath());sb.append("\r\n\r\n");textView.setText(sb.toString());}}
在AndroidManifest.xml中配置被启动的Activity:
<intent-filter> <action android:name="com.zzj.ui.OTHERACTIVITY" /> <action android:name="com.zzj.ui.INTENTACTIVITY" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="com.zzj.ui.DEFAULT" /> <category android:name="com.zzj.ui.INTENT" /> <data android:host="www.csdn.net" android:mimeType="text/html" android:path="/article" android:port="80" android:scheme="http" /> </intent-filter>
一般情况下,Intent会默认添加一个名为android.intent.category.DEFAULT的category,所以需要在intent-filter中配置。
布局文件很简单,就不贴了。下面贴出被启动的Activity的界面效果:
URI分为scheme、host、port和path四个部分,标准模式为:
scheme://host:port/path
or pathPrefix
or pathPattern
不是每个部分都是必须的。
隐式Intent需要匹配三个元素:Action、Category以及Data。
Action和Category的匹配很简单,只要保证Intent中设置的Action和Category是intent-filter中配置的Action和Category的子集就行了。
Data的匹配比较复杂,就不去研究了。下面说说如何利用Data传参。
利用Data传参
我们跳转到拨号界面时,可以传递一个电话号码,通过intent.setData(Uri data)设置。下面我们来模拟一下。
intent设置:
intent.setAction("com.zzj.ui.PHONE");intent.setData(Uri.parse("tel:13478764456"));
拨号activity类:
package com.zzj.ui.intentdemo;import android.app.Activity;import android.net.Uri;import android.os.Bundle;import android.widget.TextView;import com.zzj.ui.R;public class PhoneActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.phone_activity);Uri uri = getIntent().getData();if (uri != null) {String[] uris = uri.toString().split(":");if (uris.length > 1) {((TextView) findViewById(R.id.phone_number)).setText(uris[1]);}}}}
在AndroidManifest.xml中配置拨号Activity:
<intent-filter> <action android:name="com.zzj.ui.PHONE" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="tel" /> </intent-filter>
拨号activity布局文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/phone_number" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拨号" /></LinearLayout>
拨号界面效果:
系统自带的Action和Category
- Android Intent 简介
- 【Android】Intent简介
- Android之Intent 简介
- Android intent-fileter简介
- Android——Intent简介
- Android:Intent Filter 和 action 简介
- (转)Android:Intent Filter 和 action 简介
- android组件间信使--Intent简介
- Intent简介
- Intent简介
- Intent简介
- Intent简介
- Intent 和 Intent Filter简介
- Android中的消息传递——Intent简介
- Intent传递参数简介与android方式的全局变量
- Android Intent的简介以及属性的详解
- Android:Intent Filter 和 action 简介AndroidMainfest.xm
- android Intent的简介以及属性的详解
- 基于VLC的播放器开发
- 嵌入式 ubuntu14.04登录界面root
- Java 复习笔记 线程Thread
- Ajax核心技术之XMLHttpRequest对象
- IOS常用宏定义和讲解
- 【Android】Intent简介
- 八皇后
- iOS 中基础字符判断函数收集(如判断大小写、数字等)
- [补充二]基于多幅图像的3D重构
- butterknife的使用
- 搜索(HDU 1026)
- android mtk6592 禁止拖动主界面图标
- Java学习笔记之获取当前目录或文件
- 小波变换简介(3)