intent主要用来进行组件之间的相互启动,当然它也可以启动系统自带的程序.
一.Intent类的Java源代码中定义了Intent相关内容的变量,如下:
1.componentName(组件名称),指定Intent的目标组件的类名称。组件名称是可选的,如果填写,Intent对象会发送给指定组件名称的组件,否则也可以通过其他Intent信息定位到适合的组件。组件名称是个ComponentName类型的对象。
2.action(动作),指定Intent的执行动作,比如调用拨打电话组件。
3.data(数据),起到表示数据和数据MIME类型的作用。不同的action是和不同的data类型配套的,通过设置data的Uri来获得。
4.category(类别),被执行动作的附加信息。例如应用的启动Activity在intent-filter中设置category。
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
二.捆绑传值,使用Bundle:
- Intent it2 = new Intent(this, Main2Activity.class);
- Bundle bd = new Bundle();
- bd.putString("team", "地表最强战队");
- bd.putString("name", "周杰伦");
- it2.putExtras(bd);
- startActivity(it2);
- Intent it_get = getIntent();
- Bundle bd_get = it_get.getExtras();
- team2 = bd_get.getString("team");
- name2 = bd_get.getString("name");
三.调用系统自带的服务
// 调用浏览器
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:zuolongsnail@163.com"
);
Intent intent =
new
Intent(Intent.ACTION_SENDTO, emailUri);
// 直接发邮件
Intent intent =
new
Intent(Intent.ACTION_SEND);
String[] tos = {
"zuolongsnail@gmail.com"
};
String[] ccs = {
"zuolongsnail@163.com"
};
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);