在Android中,Intent是一个将要执行的动作的抽象的描述,一般来说是作为参数来使用,由Intent来协助完成android各个组件之间的通讯。以下列出Intent常用的用法:
1、调用WEB浏览器
- Uri uri = Uri.parse("http://www.baidu.com");
- Intent intent = new Intent(Intent.ACTION_VIEW ,uri);
- startActivity(intent);
2、在地图上显示某个坐标
- Uri uri = Uri.parse("geo:21.803233,-53.829831");
- Intent intent = new Intent(Intent.Action_VIEW, uri);
- startActivity(intent);
3、显示路径
- Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");
- Intent intent = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(intent);
4、调用拨打电话界面
- Uri uri = Uri.parse("tel:10086");
- Intent intent = new Intent(Intent.ACTION_DIAL, uri);
- startActivity(intent);
5、直接拨打电话
需要添加权限
<uses-permission id="android.permission.CALL_PHONE" />
- Uri uri = Uri.parse("tel.10086");
- Intent intent =new Intent(Intent.ACTION_CALL, uri);
6、发送短信或彩信
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.putExtra("sms_body", "The SMS text");
- intent.setType("vnd.android-dir/mms-sms");
- startActivity(intent);
7、发送短信
- Uri uri = Uri.parse("smsto:10000");
- Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
- intent.putExtra("sms_body", "haha");
- startActivity(intent);
8、发送彩信
- Uri uri = Uri.parse("content://media/external/images/media/23");
- Intent intent = new Intent(Intent.ACTION_SEND);
- intent.putExtra("sms_body", "some text");
- intent.putExtra(Intent.EXTRA_STREAM, uri);
- intent.setType("image/png");
- startActivity(intent);
或者
- Intent intent = new Intent(Intent.ACTION_SEND);
- intent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
- intent.putExtra("subject", "彩信主题");
- intent.putExtra("sms_body", "彩信内容");
- intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/images/11.jpg"));
- intent.setType("image/jpeg");
- startActivity(intent);
9、调用发送邮件功能
- Uri uri = Uri.parse("mailto:xxx@foxmail.com");
- Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
- startActivity(intent);
10、发送邮件
- Intent intent = new Intent(Intent.ACTION_SEND);
- intent.putExtra(Intent.EXTRA_EMAIL, xxx@foxmail.com);
- intent.putExtra(Intent.EXTRA_TEXT, "The email body text");
- intent.setType("text/plain");
- startActivity(Intent.createChooser(intent, "Choose Email Client"));
或者
- Intent intent = new Intent(Intent.ACTION_SEND);
- String[] tos = {"me@abc.com"};
- String[] ccs = {"you@abc.com"};
- intent.putExtra(Intent.EXTRA_EMAIL, tos);
- intent.putExtra(Intent.EXTRA_CC, ccs);
- intent.putExtra(Intent.EXTRA_TEXT, "The email body text");
- intent.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
- intent.setType("message/rfc822");
- startActivity(Intent.createChooser(intent, "Choose Email Client"));
11、播放媒体文件
- Intent intent = new Intent(Intent.ACTION_VIEW);
- Uri uri = Uri.parse(<a href="file:///sdcard/musics/someonelikeyou.mp3">file:///sdcard/musics/someonelikeyou.mp3</a>);
- intent.setDataAndType(uri, "audio/mp3");
- startActivity(intent);
- Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
- Intent intent = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(intent);
12、卸载APK
- Uri uri = Uri.fromParts("package", strPackageName, null);
- Intent intent = new Intent(Intent.ACTION_DELETE, uri);
- startActivity(intent);
或者
- Uri uninstallUri = Uri.fromParts("package", "xxx", null);
- Intent intent = new Intent(Intent.ACTION_DELETE, uninstallUri);
13、安装APK
- Uri installUri = Uri.fromParts("package", "xxx", null);
- Intent intent = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);
14、播放音乐
- Uri playUri = Uri.parse("file:///sdcard/download/sth.mp3");
- Intent intent = new Intent(Intent.ACTION_VIEW, playUri);
15、发送附件
- Intent intent = new Intent(Intent.ACTION_SEND);
- intent.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
- intent.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/test.mp3");
- sendIntent.setType("audio/mp3");
- startActivity(Intent.createChooser(intent, "Choose Email Client"));
16、market上某个应用信,pkg_name就是应用的packageName
- Uri uri = Uri.parse("market://search?q=pname:pkg_name");
- Intent intent = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(intent);
17、market上某个应用信息,app_id可以通过www网站看下
- Uri uri = Uri.parse("market://details?id=app_id");
- Intent intent = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(intent);
18、调用搜索功能
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_WEB_SEARCH);
- intent.putExtra(SearchManager.QUERY, "android");
- startActivity(intent);