Intent是不同组件之间相互通讯的纽带,封装了不同组件之间通讯的条件。
Intent对象包含的内容
在Intent类的Java源代码中定义了Intent相关内容的变量,如下:
[java] view plain copy
- // Action
- private String mAction;
- // Data
- private Uri mData;
- private String mType;
- private String mPackage;
- // ComponentName
- private ComponentName mComponent;
- // Flag
- private int mFlags;
- // category
- private HashSet<String> mCategories;
- // extras
- private Bundle mExtras;
1.componentName(组件名称),指定Intent的目标组件的类名称。组件名称是可选的,如果填写,Intent对象会发送给指定组件名称的组件,否则也可以通过其他Intent信息定位到适合的组件。组件名称是个ComponentName类型的对象。
用法:
[java] view plain copy
- Intent intent = new Intent();
- // 构造的参数为当前Context和目标组件的类路径名
- ComponentName cn = new ComponentName(HelloActivity.this, "com.byread.activity.OtherActivity");
- intent.setComponent(cn);
- startActivity(intent);
相当于以下常用方法:
[java] view plain copy
- Intent intent = new Intent();
- intent.setClass(HelloActivity.this, OtherActivity.class);
- startActivity(intent);
Intent类中也包含一个初始化ComponentName的构造函数:
[java] view plain copy
- public Intent(Context packageContext, Class<?> cls) {
- mComponent = new ComponentName(packageContext, cls);
- }
2.action(动作),指定Intent的执行动作,比如调用拨打电话组件。
[java] view plain copy
- public Intent(String action) {
- mAction = action;
- }
3.data(数据),起到表示数据和数据MIME类型的作用。不同的action是和不同的data类型配套的,通过设置data的Uri来获得。
[java] view plain copy
- public Intent(String action, Uri uri) {
- mAction = action;
- mData = uri;
- }
比如调用拨打电话组件:
[java] view plain copy
- Uri uri = Uri.parse("tel:10086");
- // 参数分别为调用拨打电话组件的Action和获取Data数据的Uri
- Intent intent = new Intent(Intent.ACTION_DIAL, uri);
- startActivity(intent);
4.category(类别),被执行动作的附加信息。例如应用的启动Activity在intent-filter中设置category。
[xhtml] view plain copy
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
5.extras(附加信息),为处理Intent组件提供附加的信息。可通过putXX()和getXX()方法存取信息;也可以通过创建Bundle对象,再通过putExtras()和getExtras()方法来存取。
6.flags(标记),指示Android如何启动目标Activity,设置方法为调用Intent的setFlags方法。常用的Flags参数有:
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_NO_HISTORY
FLAG_ACTIVITY_SINGLE_TOP
Intent的投递
1.显式方式。直接设置目标组件的ComponentName,用于一个应用内部的消息传递,比如启动另一个Activity或者一个services。
通过Intent的setComponent和setClass来制定目标组件的ComponentName。
2.隐式方式。ComponentName为空,用于调用其他应用中的组件。需要包含足够的信息,这样系统才能根据这些信息使用intent filter在所有的组件中过滤action、data或者category来匹配目标组件。可参考Android中Activity组件详解(5.Activity的Intent Filter)
如果Intent指明定了action,则目标组件的IntentFilter的action列表中就必须包含有这个action,否则不能匹配;
如果Intent没有提供type,系统将从data中得到数据类型。和action一样,目标组件的数据类型列表中必须包含Intent的数据类型,否则不能匹配;
如果Intent中的数据不是content: 类型的URI,而且Intent也没有明确指定它的type,将根据Intent中数据的scheme (比如 http: 或者mailto: ) 进行匹配。同上,Intent 的scheme必须出现在目标组件的scheme列表中;
如果Intent指定了一个或多个category,这些类别必须全部出现在组建的类别列表中。比如 Intent中包含了两个类别:LAUNCHER_CATEGORY 和 ALTERNATIVE_CATEGORY,解析得到的目标组件必须至少包含这两个类别。
Intent调用常见系统组件
[java] view plain copy
- // 调用浏览器
- Uri webViewUri = Uri.parse("http://blog.csdn.net/zuolongsnail");
- Intent intent = new Intent(Intent.ACTION_VIEW, webViewUri);
- // 调用地图
- Uri mapUri = Uri.parse("geo:100,100");
- Intent intent = new Intent(Intent.ACTION_VIEW, mapUri);
- // 播放mp3
- Uri playUri = Uri.parse("file:///sdcard/test.mp3");
- Intent intent = new Intent(Intent.ACTION_VIEW, playUri);
- intent.setDataAndType(playUri, "audio/mp3");
- // 调用拨打电话
- Uri dialUri = Uri.parse("tel:10086");
- Intent intent = new Intent(Intent.ACTION_DIAL, dialUri);
- // 直接拨打电话,需要加上权限<uses-permission id="android.permission.CALL_PHONE" />
- Uri callUri = Uri.parse("tel:10086");
- Intent intent = new Intent(Intent.ACTION_CALL, callUri);
- // 调用发邮件(这里要事先配置好的系统Email,否则是调不出发邮件界面的)
- Uri emailUri = Uri.parse("mailto:[email protected]");
- Intent intent = new Intent(Intent.ACTION_SENDTO, emailUri);
- // 直接发邮件
- Intent intent = new Intent(Intent.ACTION_SEND);
- String[] tos = { "[email protected]" };
- String[] ccs = { "[email protected]" };
- intent.putExtra(Intent.EXTRA_EMAIL, tos);
- intent.putExtra(Intent.EXTRA_CC, ccs);
- intent.putExtra(Intent.EXTRA_TEXT, "the email text");
- intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
- intent.setType("text/plain");
- Intent.createChooser(intent, "Choose Email Client");
- // 发短信
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.putExtra("sms_body", "the sms text");
- intent.setType("vnd.android-dir/mms-sms");
- // 直接发短信
- Uri smsToUri = Uri.parse("smsto:10086");
- Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
- intent.putExtra("sms_body", "the sms text");
- // 发彩信
- Uri mmsUri = Uri.parse("content://media/external/images/media/23");
- Intent intent = new Intent(Intent.ACTION_SEND);
- intent.putExtra("sms_body", "the sms text");
- intent.putExtra(Intent.EXTRA_STREAM, mmsUri);
- intent.setType("image/png");
- // 卸载应用
- Uri uninstallUri = Uri.fromParts("package", "com.app.test", null);
- Intent intent = new Intent(Intent.ACTION_DELETE, uninstallUri);
- // 安装应用
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.setDataAndType(Uri.fromFile(new File("/sdcard/test.apk"), "application/vnd.android.package-archive");
- // 在Android Market中查找应用
- Uri uri = Uri.parse("market://search?q=愤怒的小鸟");
- Intent intent = new Intent(Intent.ACTION_VIEW, uri);
注意:有的需要配置一定的权限