一些Intent使用例子

[代码] 调用拨号程序
1    // 给移动客服10086拨打电话
2    Uri uri = Uri.parse("tel:10086");
3    Intent intent = new Intent(Intent.ACTION_DIAL, uri);
4    startActivity(intent);
[代码] 发送短信或彩信
01    // 给10086发送内容为“Hello”的短信
02    Uri uri = Uri.parse("smsto:10086");
03    Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
04    intent.putExtra("sms_body", "Hello");
05    startActivity(intent);
06    // 发送彩信(相当于发送带附件的短信)
07    Intent intent = new Intent(Intent.ACTION_SEND);
08    intent.putExtra("sms_body", "Hello");
09    Uri uri = Uri.parse("content://media/external/images/media/23");
10    intent.putExtra(Intent.EXTRA_STREAM, uri);
11    intent.setType("image/png");
12    startActivity(intent);
[代码] 通过浏览器打开网页
1    // 打开Google主页
2    Uri uri = Uri.parse("http://www.google.com");
3    Intent intent  = new Intent(Intent.ACTION_VIEW, uri);
4    startActivity(intent);
[代码] 发送电子邮件
01    // 给someone@domain.com发邮件
02    Uri uri = Uri.parse("mailto:someone@domain.com");
03    Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
04    startActivity(intent);
05    // 给someone@domain.com发邮件发送内容为“Hello”的邮件
06    Intent intent = new Intent(Intent.ACTION_SEND);
07    intent.putExtra(Intent.EXTRA_EMAIL, "someone@domain.com");
08    intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
09    intent.putExtra(Intent.EXTRA_TEXT, "Hello");
10    intent.setType("text/plain");
11    startActivity(intent);
12    // 给多人发邮件
13    Intent intent=new Intent(Intent.ACTION_SEND);
14    String[] tos = {"1@abc.com", "2@abc.com"}; // 收件人
15    String[] ccs = {"3@abc.com", "4@abc.com"}; // 抄送
16    String[] bccs = {"5@abc.com", "6@abc.com"}; // 密送
17    intent.putExtra(Intent.EXTRA_EMAIL, tos);
18    intent.putExtra(Intent.EXTRA_CC, ccs);
19    intent.putExtra(Intent.EXTRA_BCC, bccs);
20    intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
21    intent.putExtra(Intent.EXTRA_TEXT, "Hello");
22    intent.setType("message/rfc822");
23    startActivity(intent);
[代码] 显示地图与路径规划
1    // 打开Google地图中国北京位置(北纬39.9,东经116.3)
2    Uri uri = Uri.parse("geo:39.9,116.3");
3    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
4    startActivity(intent);
5    // 路径规划:从北京某地(北纬39.9,东经116.3)到上海某地(北纬31.2,东经121.4)
6    Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=39.9 116.3&daddr=31.2 121.4");
7    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
8    startActivity(intent);
[代码] 播放多媒体
1    Intent intent = new Intent(Intent.ACTION_VIEW);
2    Uri uri = Uri.parse("file:///sdcard/foo.mp3");
3    intent.setDataAndType(uri, "audio/mp3");
4    startActivity(intent);
5     
6    Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
7    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
8    startActivity(intent);
[代码] 拍照
1    // 打开拍照程序
2    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
3    startActivityForResult(intent, 0);
4    // 取出照片数据
5    Bundle extras = intent.getExtras();
6    Bitmap bitmap = (Bitmap) extras.get("data");
[代码] 获取并剪切图片
01    // 获取并剪切图片
02    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
03    intent.setType("image/*");
04    intent.putExtra("crop", "true"); // 开启剪切
05    intent.putExtra("aspectX", 1); // 剪切的宽高比为1:2
06    intent.putExtra("aspectY", 2);
07    intent.putExtra("outputX", 20); // 保存图片的宽和高
08    intent.putExtra("outputY", 40);
09    intent.putExtra("output", Uri.fromFile(new File("/mnt/sdcard/temp"))); // 保存路径
10    intent.putExtra("outputFormat", "JPEG");// 返回格式
11    startActivityForResult(intent, 0);
12    // 剪切特定图片
13    Intent intent = new Intent("com.android.camera.action.CROP");
14    intent.setClassName("com.android.camera", "com.android.camera.CropImage");
15    intent.setData(Uri.fromFile(new File("/mnt/sdcard/temp")));
16    intent.putExtra("outputX", 1); // 剪切的宽高比为1:2
17    intent.putExtra("outputY", 2);
18    intent.putExtra("aspectX", 20); // 保存图片的宽和高
19    intent.putExtra("aspectY", 40);
20    intent.putExtra("scale", true);
21    intent.putExtra("noFaceDetection", true);
22    intent.putExtra("output", Uri.parse("file:///mnt/sdcard/temp"));
23    startActivityForResult(intent, 0);
[代码] 打开Google Market
1    // 打开Google Market直接进入该程序的详细页面
2    Uri uri = Uri.parse("market://details?id=" + "com.demo.app");
3    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
4    startActivity(intent);
[代码] 安装和卸载程序
1    Uri uri = Uri.fromParts("package", "com.demo.app", null); 
2    Intent intent = new Intent(Intent.ACTION_DELETE, uri); 
3    startActivity(intent);
[代码] 进入设置界面
1    // 进入无线网络设置界面(其它可以举一反三) 
2    Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS); 
3    startActivityForResult(intent, 0);
posted @ 2012-04-18 12:52  Yunzhu  阅读(172)  评论(0编辑  收藏  举报