首页 > 学院 > 开发设计 > 正文

再看Intent.putExtra

2019-11-06 09:49:03
字体:
来源:转载
供稿:网友

仅作学习笔记,大神请指点

以前对putExtra的理解只是Intent携带的以键值对形式存在的信息,键对值进行一个标记,以便到了对方那里还能将值提取出来,许多教材中对这方面的知识讲解的不够透彻,许多技术博客上也只是讲了键值对之类的基础知识,很少有仔细讲‘键’的作用的,其实这个键可以做很多事情,比如如果键为MediaStore.EXTRA_OUTPUT,那么将值设置为一个Uri对象就可以将启动相机拍摄的照片保存到Uri对象中,这个键是MediaStore中定义的一个常量,程序遇到键为这个常量的值就知道该怎么做,像这样的常量还有很多,标志着程序遇到这个常量时无需程序员手动操作就可以完成许多任务。

收录网上的一些Intent用法:

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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表