Intent是一种运行时绑定机制,它的作用是在程序运行是连接Activity、Service和BroadcastReceiver。
Intent本意是“请求,意图”,通过Intent可以在程序件跳转和传递数据,Intent包含了具体的请求信息,针对不同的组件包含的信息有所不同,且不同组件的启动方式也不同,一个Intent就是一次对将要执行的操作的抽象描述。
Intent启动组件的方式如下
启动Activity
Content.startActivity(Intent intent)
Activity.startActivityForResult(Intentintent,int requestCode)
启动Service
Context.startService(Intentintent)
Context.bindService(Intentintent,ServiceConnection conn,int flags)
发送广播
Context.sentBroadcast(Intentintent)
Context.sendOrderedBroadcast(Intentintent,String recevierPermission)
Context.sendStickyBroadcast(Intentintent)
Intent由以下部分组成
Action动作
Data数据
Type数据类型
Category类别:动作附加信息
Extra附加信息:其他附加信息
Component目标组件
Intent分显式和隐式。显示Intent即明确标明目标组件的方式,我们做页面切换的实验都是用显示Intent。隐式Intent就是没有明确指出目标组件的方式,这个方式需要在配置文件中做一些配置来确定目标组件的类型,通过隐式Intent我们就可以实现在本程序调用其他程序。
隐式Intent主要用到配置文件(AndroidManifest.xml)中的<intent-filter>元素,通过配置它的属性和元素来设定组件希望接收什么类型的请求行为,以及什么类型的请求数据。
以下通过Activity做详细的代码测试,准备三个界面,第二三个界面仅用来跳转。
显示Intent测试
布局文件
第一个界面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.briup.intent.MainActivity" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="GotoSecond"
android:onClick="gotoSecond" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="GotoThird"
android:onClick="gotoThird" />
</LinearLayout>
第二个界面
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.briup.intent.SecoActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
第三个界面
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.briup.intent.ThirActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
Activity类文件
第一个
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void gotoSecond(View v){
//第一种显示Intent启动Activity方式
Intent intent = new Intent();
ComponentName cn = new ComponentName(this,SecoActivity.class);
intent.setComponent(cn);
startActivity(intent);
}
public void gotoThird(View v){
Intent intent = new Intent(this,SecoActivity.class);//第三种,最常用
startActivity(intent);
}
}
第二个
import android.app.Activity;
import android.os.Bundle;
public class SecoActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seco);
}
}
第三个
import android.app.Activity;
import android.os.Bundle;
public class ThirActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_thir);
}
}
不需要设置配置文件,效果如下
可以看到顺利完成跳转。
隐式Intent测试,以下仅对第一个Activity类文件配置文件做简单的修改进行测试
1.action
通过在调用方用Intent.setAction()方法和在被调用方的配置文件中添加<action>设置相同的action名来绑定它们之间的跳转,值得注意的是,如果要使用Action必须同时配置category元素,如果没有附加信息必须使用默认值android.intent.category.DEFAULT。
注意:一个<intent-filter>中可以包含多个<action>也就是说它是可以响应多个事件,但是setAction()方法一个代码块只能有一个,也就是说一种情况只能激发一种事件。但是如果多个Activity的<intent-filter>中都有相同的<action>系统会把这些Activity的名字列出供用户选择。
Activity类如下
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void gotoSecond(View v){
Intent intent = new Intent();
intent.setAction("briup");
startActivity(intent);
}
public void gotoThird(View v){
Intent intent = new Intent(this,SecoActivity.class);
startActivity(intent);
}
}
配置文件如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.briup.intent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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=".SecoActivity"
android:label="@string/title_activity_seco" >
<intent-filter>
<action android:name="briup" />
<action android:name="garen" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".ThirActivity"
android:label="@string/title_activity_thir"
>
</intent-filter>
</activity>
</application>
</manifest>
效果如下
如果要消除这种选择,可以通过为它们设置优先级(priority)来确定选择哪一个,priority属性的值为-1000~1000,数值越大优先级越高,但是想不出现选择必须把不选的优先级设为负数,要选的设为正数,系统将自动跳转到优先级为正数的界面。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.briup.intent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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=".SecoActivity"
android:label="@string/title_activity_seco" >
<intent-filter android:priority="10">
<action android:name="briup" />
<action android:name="garen" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".ThirActivity"
android:label="@string/title_activity_thir"
>
<intent-filter android:priority="10">
<action android:name="briup" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
2.Category
相似的通过在调用方用方法Intent.addCategory()方法和在被调用方的配置文件中添加<category>设置相同的附加信息,就可以为它们的跳转添加判断条件。
注意:一个<intent-filter>中可以包含多个<category>,一个代码块也可以有多个addCategory()方法,要想正确跳转界面,必须设置其中专属的category名,也可以全部设置。
Activity类如下
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void gotoSecond(View v){
Intent intent = new Intent();
intent.setAction("briup");
intent.addCategory("cater");
intent.addCategory("timor");//满足一个即可
startActivity(intent);
}
public void gotoThird(View v){
Intent intent = new Intent(this,SecoActivity.class);
startActivity(intent);
}
}
配置文件如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.briup.intent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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=".SecoActivity"
android:label="@string/title_activity_seco" >
<intent-filter>
<action android:name="briup" />
<action android:name="garen" />
<category android:name="cater"/>
<category android:name="timor"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".ThirActivity"
android:label="@string/title_activity_thir"
>
</activity>
</application>
</manifest>
效果是一样的。
3.Data和Type
Extra已经在Activity数据传递中做了详细实验。Data的使用可以帮助我们跳转到其他的应用程序,有些还需要Type来控制执行的类型。当然,这种方法也可以跳转到本程序的界面。
首先,Data传递的是一个URI地址,在Java代码(Intent.setData())和配置文件中需要一一对应,配置文件中需要将URI地址分为四部分来配置(协议、IP地址、端口、应用程序包名),Java代码中则需要通过URI解析器解析URI字符串Uri.parse()方法。如果配置文件中配置的URI地址不完整,Java代码中的URI地址只要和配置文件中有的部分一致,其他可有可无,也能达到跳转效果。如果配置文件中为Data配置了Type,那么Java代码中需要通过setDataAndType()方法对Data和Type同时配置,如果分别用setData()和setType()方法分别设置,前面一个会被后面一个覆盖,无法有效设置。
以下做一个简单的测试,使上面的程序可以跳转到本程序界面、跳转到网页、跳转到音乐播放器、跳转到拨号界面等,这样的跳转需要通过Action和Data的结合完成。当然每执行一种跳转都需要特定的Action,这样的Action系统已经为我们准备好了,直接调用静态属性即可。
布局文件如下
第一个界面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.briup.intent.MainActivity" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="GotoSecond"
android:onClick="gotoSecond" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="GotoThird"
android:onClick="gotoThird" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转到网页"
android:onClick="page"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="播放音乐"
android:onClick="music"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="拨打电话"
android:onClick="call"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="卸载"
android:onClick="delete"/>
</LinearLayout>
第二三个界面同上
配置文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.briup.intent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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=".SecoActivity"
android:label="@string/title_activity_seco" >
<intent-filter >
<action android:name="briup" />
<action android:name="garen" />
<category android:name="cater"/>
<category android:name="timor"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".ThirActivity"
android:label="@string/title_activity_thir"
>
<intent-filter >
<action android:name="briup" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="briup"
android:host="127.0.0.1"
android:port="8888"
android:path="/abc"
android:mimeType="qwe/rty"/>
</intent-filter>
</activity>
</application>
</manifest>
Activity类
第一个
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void gotoSecond(View v){
Intent intent = new Intent();
intent.setAction("briup");
intent.addCategory("cater");
intent.addCategory("timor");//满足一个即可
startActivity(intent);
}
//跳转至本程序界面,需要与配置文件匹配
public void gotoThird(View v){
Intent intent = new Intent();
//将字符串转换成URI对象
Uri uri = Uri.parse("briup://127.0.0.1:8888/abc");
//intent.setData(uri);
//intent.setType("qwe/rty");//两个方法不能分开设置,否则后一个会覆盖前一个
intent.setDataAndType(uri, "qwe/rty");
startActivity(intent);
}
//跳转至网页,需要网址
public void page(View v){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);//显示指定数据
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);
}
//拨打电话,需要电话号码
public void call(View v){
Intent intent = new Intent();
//intent.setAction(Intent.ACTION_CALL);//直接拨号
intent.setAction(Intent.ACTION_DIAL);//跳转到拨号界面
intent.setData(Uri.parse("tel:18862001111"));
startActivity(intent);
}
//播放音乐,需要音乐路径
public void music(View v){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file:///sdcard/befor.mp3"),"audio/mp3");
startActivity(intent);
}
//卸载程序,需要完整包名
public void delete(View v){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_DELETE);//删除数据
intent.setData(Uri.parse("package:com.briup.activity"));
startActivity(intent);
}
}
第二三个同上
效果如下