Android基础入门教程——4.5.1 Intent的基本使用

标签(空格分隔): Android基础入门教程


本节引言:

在上一节结束后意味着Android的四大组件我们都已经学习完毕了~,而本节我们要学习的是四大组件间的
枢纽——Intent(意图),Android通信的桥梁,比如我们可以通过:

  • startActivity(Intent)/startActivityForResult(Intent):来启动一个Activity
  • startService(Intent)/bindService(Intent):来启动一个Service
  • sendBroadcast:发送广播到指定BroadcastReceiver
  • 另外别忘了我们在注册四大组件时,写得很多的Intent-Filter哦~

好吧,话不多说,开始本节内容!另外前面我们已经用过Intent了,就不在讲述概念性的东西了~
老规矩,官方API:Intent


1.显式Intent与隐式Intent的区别

  • 显式Intent:通过组件名指定启动的目标组件,比如startActivity(new Intent(A.this,B.class));
    每次启动的组件只有一个~
  • 隐式显式Intent:不指定组件名,而指定Intent的Action,Data,或Category,当我们启动组件时,
    会去匹配AndroidManifest.xml相关组件的Intent-filter,逐一匹配出满足属性的组件,当不止一个满足时,
    会弹出一个让我们选择启动哪个的对话框~

2.Intent的七个属性:


1)ComponentName(组件名称)


2)Action(动作)


3)Category(类别)


4)Data(数据),Type(MIME类型)


5)Extras(额外)


6)Flags(标记)


3.显式Intent使用示例:

这个用得很多,直接就上例子了:

例子1:点击按钮返回Home界面:
运行效果图:

核心代码:

Intent it = new Intent();
it.setAction(Intent.ACTION_MAIN);
it.addCategory(Intent.CATEGORY_HOME);
startActivity(it);

例子2:点击按钮打开百度页面:
运行效果图:

核心代码:

Intent it = new Intent();
it.setAction(Intent.ACTION_VIEW);
it.setData(Uri.parse("http://www.baidu.com"));
startActivity(it);

4.隐式Intent详解


1)预定义动作的隐式Intent示例:

代码示例:点击按钮后,所有Action为VIEW的Activity被筛选出来,由用户进一步选择:

核心代码:

建立第二个Activity的布局,与对应的Activity,在第一个Activity的按钮点击事件中添加一下代码:

Intent it = new Intent();
it.setAction(Intent.ACTION_VIEW);
startActivity(it);

最后在第二个Activity的Intent中添加以下代码:

 <activity android:name=".SecondActivity"android:label="第二个Activity"><intent-filter><action android:name="android.intent.action.VIEW"/><category android:name="android.intent.category.DEFAULT"/></intent-filter>           </activity>

运行效果图:


2)自定义动作的隐式Intent示例:

代码示例:使用自定义的Action与category来激活另一个Activity

核心代码:
建立第二个Activity的布局,与对应的Activity,在第一个Activity的按钮点击事件中添加一下代码:

Intent it = new Intent();
it.setAction("my_action");
it.addCategory("my_category");
startActivity(it);  

最后在第二个Activity的Intent中添加以下代码:

<activity android:name=".SecondActivity"android:label="第二个Activity"><intent-filter><action android:name="my_action"/><category android:name="my_category"/><category android:name="android.intent.category.DEFAULT"/></intent-filter>
</activity>

注意虽然我们自定义了一个category,但是还是要把这个默认的加上,不然会报错的:

<category android:name="android.intent.category.DEFAULT"/>

5.常用系统Intent合集

大家贴下常用的系统Intent的合集吧,上面没有的欢迎提出~

//===============================================================
//1.拨打电话
// 给移动客服10086拨打电话
Uri uri = Uri.parse("tel:10086");
Intent intent = new Intent(Intent.ACTION_DIAL, uri);
startActivity(intent);//===============================================================//2.发送短信
// 给10086发送内容为“Hello”的短信
Uri uri = Uri.parse("smsto:10086");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "Hello");
startActivity(intent);//3.发送彩信(相当于发送带附件的短信)
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);//===============================================================//4.打开浏览器:
// 打开百度主页
Uri uri = Uri.parse("http://www.baidu.com");
Intent intent  = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);//===============================================================//5.发送电子邮件:(阉割了Google服务的没戏!!!!)
// 给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);//===============================================================//6.显示地图:
// 打开Google地图中国北京位置(北纬39.9,东经116.3)
Uri uri = Uri.parse("geo:39.9,116.3");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);//===============================================================//7.路径规划
// 路径规划:从北京某地(北纬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);//===============================================================//8.多媒体播放:
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/foo.mp3");
intent.setDataAndType(uri, "audio/mp3");
startActivity(intent);//获取SD卡下所有音频文件,然后播放第一首=-=
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);//===============================================================//9.打开摄像头拍照:
// 打开拍照程序
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(MediaStore.ACTION_IMAGE_CAPTURE);
time = Calendar.getInstance().getTimeInMillis();
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment
.getExternalStorageDirectory().getAbsolutePath()+"/tucue", time + ".jpg")));
startActivityForResult(intent, ACTIVITY_GET_CAMERA_IMAGE);//===============================================================//10.获取并剪切图片
// 获取并剪切图片
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);//===============================================================//11.打开Google Market
// 打开Google Market直接进入该程序的详细页面
Uri uri = Uri.parse("market://details?id=" + "com.demo.app");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);//===============================================================//12.进入手机设置界面:
// 进入无线网络设置界面(其它可以举一反三)
Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
startActivityForResult(intent, 0);//===============================================================//13.安装apk:
Uri installUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);//===============================================================//14.卸载apk:
Uri uri = Uri.fromParts("package", strPackageName, null);
Intent it = new Intent(Intent.ACTION_DELETE, uri);
startActivity(it); //===============================================================//15.发送附件:
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/eoe.mp3");
sendIntent.setType("audio/mp3");
startActivity(Intent.createChooser(it, "Choose Email Client"));//===============================================================//16.进入联系人页面:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(People.CONTENT_URI);
startActivity(intent);//===============================================================//17.查看指定联系人:
Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, info.id);//info.id联系人ID
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(personUri);
startActivity(intent);//===============================================================//18.调用系统编辑添加联系人(高版本SDK有效):
Intent it = newIntent(Intent.ACTION_INSERT_OR_EDIT);
it.setType("vnd.android.cursor.item/contact");
//it.setType(Contacts.CONTENT_ITEM_TYPE);
it.putExtra("name","myName");
it.putExtra(android.provider.Contacts.Intents.Insert.COMPANY, "organization");
it.putExtra(android.provider.Contacts.Intents.Insert.EMAIL,"email");
it.putExtra(android.provider.Contacts.Intents.Insert.PHONE,"homePhone");
it.putExtra(android.provider.Contacts.Intents.Insert.SECONDARY_PHONE,"mobilePhone");
it.putExtra( android.provider.Contacts.Intents.Insert.TERTIARY_PHONE,"workPhone");
it.putExtra(android.provider.Contacts.Intents.Insert.JOB_TITLE,"title");
startActivity(it);//===============================================================//19.调用系统编辑添加联系人(全有效):
Intent intent = newIntent(Intent.ACTION_INSERT_OR_EDIT);
intent.setType(People.CONTENT_ITEM_TYPE);
intent.putExtra(Contacts.Intents.Insert.NAME, "My Name");
intent.putExtra(Contacts.Intents.Insert.PHONE, "+1234567890");
intent.putExtra(Contacts.Intents.Insert.PHONE_TYPE,Contacts.PhonesColumns.TYPE_MOBILE);
intent.putExtra(Contacts.Intents.Insert.EMAIL, "com@com.com");
intent.putExtra(Contacts.Intents.Insert.EMAIL_TYPE, Contacts.ContactMethodsColumns.TYPE_WORK);
startActivity(intent);//===============================================================//20.打开另一程序
Intent i = new Intent();
ComponentName cn = new ComponentName("com.example.jay.test",
"com.example.jay.test.MainActivity");
i.setComponent(cn);
i.setAction("android.intent.action.MAIN");
startActivityForResult(i, RESULT_OK);//===============================================================//21.打开录音机
Intent mi = new Intent(Media.RECORD_SOUND_ACTION);
startActivity(mi);//===============================================================//22.从google搜索内容
Intent intent = new Intent();
intent.setAction(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY,"searchString")
startActivity(intent);//===============================================================

6.Action在哪里查?

本来想直接贴以前收集到的Intent Action的,后来想想还是算了,授之以鱼,还不如授之以渔,
如果你下载了Android的文档的话,可以在下述路径:
sdk–>docs–>reference–>android—>content—>Intent.html
找到这个玩意,然后从这个Constants开始就是了:

遇到陌生的自己来这里查即可~


本节小结:

好的,关于Intent的基本使用就到这里,下一节我们会来继续学习在日常开发中使用Intent可能会遇到
的一些问题或者说需求吧,敬请期待,谢谢~

Android基础入门教程——4.5.1 Intent的基本使用相关推荐

  1. 最新Android基础入门教程目录(完结版)

    第一章:环境搭建与开发相关(已完结 10/10) https://blog.csdn.net/coder_pig/article/details/50000773 Android基础入门教程--1.1 ...

  2. 2015年最新Android基础入门教程目录(完结版)

    2015年最新Android基础入门教程目录(完结版) 标签(空格分隔): Android基础入门教程 前言: 关于<2015年最新Android基础入门教程目录>终于在今天落下了帷幕,全 ...

  3. Android基础入门教程——4.3.1 BroadcastReceiver牛刀小试

    Android基础入门教程--4.3.1 BroadcastReceiver牛刀小试 标签(空格分隔): Android基础入门教程 本节引言 本节我们将来学习Android四大组件中的第三个:Bro ...

  4. Android基础入门教程——10.1 TelephonyManager(电话管理器)

    Android基础入门教程--10.1 TelephonyManager(电话管理器) 标签(空格分隔): Android基础入门教程 本节引言: 本章节是Android基础入门教程的最后一章,主要讲 ...

  5. android9壁纸存放路径,Android基础入门教程——10.9 WallpaperManager(壁纸管理器)

    Android基础入门教程--10.9 WallpaperManager(壁纸管理器) 标签(空格分隔): Android基础入门教程 本节引言: 本节给大家带来的是WallpaperManager( ...

  6. Android基础入门教程——4.2.3 Service精通

    Android基础入门教程--4.2.3 Service精通 标签(空格分隔): Android基础入门教程 本节引言: 本节,我们继续来研究Service(服务)组件,本节将会学习下Android中 ...

  7. Android基础入门教程——2.5.3 AlertDialog(对话框)详解

    Android基础入门教程--2.5.3 AlertDialog(对话框)详解 标签(空格分隔): Android基础入门教程 本节引言: 本节继续给大家带来是显示提示信息的第三个控件AlertDia ...

  8. Android基础入门教程——10.9 WallpaperManager(壁纸管理器)

    Android基础入门教程--10.9 WallpaperManager(壁纸管理器) 标签(空格分隔): Android基础入门教程 本节引言: 本节给大家带来的是WallpaperManager( ...

  9. android 编辑9图片,Android基础入门教程——1.6 .9(九妹)图片怎么玩

    Android基础入门教程--1.6 .9(九妹)图片怎么玩 Android基础入门教程 1.本节引言: 可能有的一些疑问: 1.什么是.9图片? 答:图片后缀名前有.9的图片,如pic1.9.png ...

  10. android设置webview缓存目录,Android基础入门教程——7.5.5 WebView缓存问题

    Android基础入门教程--7.5.5 WebView缓存问题 Android基础入门教程 本节引言:现在很多门户类信息网站,比如虎嗅,ifanr,钛媒体等等的APP,简单点说是信息阅读类的APP, ...

最新文章

  1. 自主研发:3通道1GS/s FPGA+DSP板
  2. CodeForces - 1144F搜索+简单图论
  3. Php的https方法,php获取https协议内容的两种方法
  4. Python+django网页设计入门(6):文件上传与数据导入
  5. 彩色静电植绒印花工艺的五个方法
  6. BAT中取得一个命令的执行结果
  7. wordpress建立php站点地图,WordPress站点地图(sitemap)制作方法(免插件)
  8. CCF CSP 归一化处理
  9. windows7 下安装 kali Linux 双系统
  10. 深入研究Espresso的Idling Resource
  11. pix2pixHD:High-Resolution Image Synthesis and Semantic Manipulation with Conditional GANs
  12. 大学以来到大二的自我评价
  13. win10下QT5.11.1静态编译(带MYSQL)
  14. 八楼电梯的c语言程序,本科毕业设计—电梯自动语音提示系统.doc
  15. 【服务器】Linux一键安装web环境全攻略(阿里云服务器)(Centos )
  16. php laravel lumen ELK日志采集系统安装
  17. IEEE 国际自动化科学与工程会议 (CASE2023)
  18. 【强大的数据迁移和恢复解决方案】KernelApps及其产品介绍
  19. 机器学习-过拟合、正则化、稀疏性、交叉验证概述
  20. 一时兴起之matlab学习记录

热门文章

  1. 表格如何调出好看的样式?
  2. 五分钟搞定单片机keil5 运行破解安装教程
  3. js实现爬取QQ群管理页面所有QQ群成员信息
  4. 最全TCP/UDP端口号整理
  5. 怎么在WINDOWS中运行C语言程序,在Windows下运行C语言程序
  6. 怎样知道android的手机号码,怎么知道自己的手机号
  7. 统计当前文件夹下pdf文件的页码总数
  8. 李宏毅机器学习笔记【未完】
  9. 汉王ocr sdk android,汉王标准印刷体OCR SDK技术白皮书.doc
  10. Pascal voc 2012 数据集简介