一起学Android之Intent
本文简述在Android开发中Intent的常见应用,仅供学习分享使用。
什么是Intent?
Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。因此,Intent在这里起着一个媒体中介的作用,专门提供组件互相调用的相关信息,实现调用者与被调用者之间的解耦。【官方文档:An intent is an abstract description of an operation to be performed(执行某操作的抽象描述)】
Intent的用途有哪些?
关联不同的组件:
- 用来激活启动其他应用程序的组件。
- 作为传递数据和事件的桥梁。
Intent调用模式
- 显式Intent:直接指定目标组件的ComponentNamae,适合启动同一个应用中的其他组件,比如在某应用程序内,一个Activity启动一个Service。
- 隐式Intent:不直接指定目标组件的ComponentName Class,适合启动设备中不同应用中的组件。
隐式Intent常见例子
打开地图:
//打开地图
public void open_map(View v) {
// Map point based on address
//Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
// Or map point based on latitude/longitude
Uri location = Uri.parse("geo:34.9501439901632,114.95770290767824?z=14"); // z param is zoom level
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
startActivity(mapIntent);
}
打开网页:
//打开指定的网页
public void open_url(View v){
Uri webpage = Uri.parse("http://www.baidu.com");
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
startActivity(webIntent);
}
打电话
//打电话
public void open_tel(View v){
Uri number = Uri.parse("tel:10086");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
startActivity(callIntent);
}
日历上设置日程
//设置日历事件
public void open_calendar(View v){
Intent calendarIntent = new Intent(Intent.ACTION_INSERT, CalendarContract.Events.CONTENT_URI);
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 0, 19, 7, 30);
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 0, 19, 10, 30);
calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis());
calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis());
calendarIntent.putExtra(CalendarContract.Events.TITLE, "Ninja class");
calendarIntent.putExtra(CalendarContract.Events.EVENT_LOCATION, "Secret dojo");
startActivity(calendarIntent);
}
判断Intent是否有接收App
public void open_chkintent(View v){
Intent calendarIntent = new Intent(Intent.ACTION_INSERT, CalendarContract.Events.CONTENT_URI);
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 0, 19, 7, 30);
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 0, 19, 10, 30);
calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis());
calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis());
calendarIntent.putExtra(CalendarContract.Events.TITLE, "Ninja class");
calendarIntent.putExtra(CalendarContract.Events.EVENT_LOCATION, "Secret dojo");
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(calendarIntent, 0);
boolean isIntentSafe = activities.size() > 0;
String msg=isIntentSafe?"有合适的接收":"没有合适的接收";
Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
}
显式调用Intent
显式调用并传递参数
//打开一个Activity
public void open_activity_param(View v){
Intent intent =new Intent(this,OtherActivity.class);
intent.putExtra("Name","Alan.hsiang");
intent.putExtra("Age",100);
intent.putExtra("Sex",true);
startActivity(intent);
}
新的Activity获取参数并展示
Intent intent=getIntent();
if(intent!=null){
if( intent.hasExtra("Name")) {
String name = intent.getStringExtra("Name");
Integer age = intent.getIntExtra("Age", 0);
Boolean sex = intent.getBooleanExtra("Sex", true);
Log.i("DemoIntent", "onCreate: "+name+"--"+age+"--"+sex+" ");
EditText txtName = (EditText) this.findViewById(R.id.txt_name);
txtName.setText(name);
Log.i("DemoIntent", "onCreate: txtName ");
EditText txtAge = (EditText) this.findViewById(R.id.txt_age);
txtAge.setText(age.toString());//此处要转换成字符串,否则会被当成id,从而报错
Log.i("DemoIntent", "onCreate: txtAge ");
RadioButton rbMale = (RadioButton) this.findViewById(R.id.rb_male);
RadioButton rbFemale = (RadioButton) this.findViewById(R.id.rb_female);
Log.i("DemoIntent", "onCreate: rbButton ");
rbMale.setChecked(sex);
rbFemale.setChecked(!sex);
}
}
备注
Intent的用途还有很多,本文旨在抛砖引玉,希望大家共同学习。
一起学Android之Intent的更多相关文章
- 从零开始学android开发-详细谈谈intent的startActivityForResult()方法
1.两种实现activity跳转的方法 实现activity的跳转主要有两种方法,startActivity()和startActivityForResult();例如activity A跳转到act ...
- Android开发学习之路-该怎么学Android(Service和Activity通信为例)
在大部分地方,比如书本或者学校和培训机构,教学Android的方式都基本类似,就是告诉先上原理方法,然后对着代码讲一下. 但是,这往往不是一个很好的方法,为什么? ① 学生要掌握这个方法的用途,只能通 ...
- 一步一步学android控件(之十五) —— DegitalClock & AnalogClock
原本计划DigitalClock和AnalogClock单独各一篇来写,但是想想,两个控件的作用都一样,就和在一起写一篇了. DegitalClock和AnalogClock控件主要用于显示当前时间信 ...
- 一步一步学android控件(之十六)—— CheckBox
根据使用场景不同,有时候使用系统默认的CheckBox样式就可以了,但是有时候就需要自定义CheckBox的样式.今天主要学习如何自定义CheckBox样式.在CheckBox状态改变时有时需要做一些 ...
- 从零開始学android<数据存储(1)SharedPreferences属性文件.三十五.>
在android中有五种保存数据的方法.各自是: Shared Preferences Store private primitive data in key-value pairs. 相应属性的键值 ...
- 一步一步学android控件(之六) —— MultiAutoCompleteTextView
今天学习的控件是MultiAutoCompleteTextView . 提到MultiAutoCompleteTextView 我们就自然而然地想到AutoCompleteTextView ,就想知道 ...
- 从零开始学android -- Service
废话不多说了,Service是四大组件之一,是一个后台处理长时间运行在主线程不需要依赖ui界面显示的应用组件,切记不能在service中做耗时操作,会阻塞主线程,要做也要在service中开个子线程做 ...
- Android笔记---Intent实现Activity跳转
学了之前的Android控件以及布局,我们就能够做一些UI的设计了,这里我结合之前的知识.以一个小的登录项目来解说下Activity之间跳转. 先看下效果图: 1.登录界面: 2.点击登录按钮跳转到另 ...
- 对照 Android 的 Intent 与 iOS StoryBoard 的 Segue - Intent 假设也能添加个prepareForSegue回调就好了
对照 Android 的 Intent 与 iOS StoryBoard 的 Segue - Intent 假设也能添加个prepareForSegue回调就好了 太阳火神的漂亮人生 (http:// ...
随机推荐
- 初探奥尔良(Orleans)
由于工作上关系目前经常被各种并发数据问题搞得焦头烂额,要么要性能舍弃数据上得一致性,要么要一致性但是却得到了特别糟糕的响应.难道鱼和熊掌真的无法兼得吗? 然后找到了类似奥尔良这种基于Actor模型的k ...
- centos 修改hostname
centos修改主机名的正确方法 1 centos6下修改hostname [root@centos6 ~]$ hostname # 查看当前的hostnmae centos6.magedu.com ...
- 第四节 pandas 数据加载
pandas提供了一些用于将表格型数据读取为DataFrame对象的函数,其中read_csv和read_table这两个使用最多. #导包import pandas as pd from panda ...
- Android values资源的定义
Android values资源是Xml格式的文件 上图定义了颜色(colors)字符串(strings)样式(style)三个资源文件 xml文件写在resources标签里 <?xml ve ...
- 【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限
开发环境搭建参见<[原]无脑操作:IDEA + maven + SpringBoot + JPA + Thymeleaf实现CRUD及分页> 需求: ① 除了登录页面,在地址栏直接访问其他 ...
- navicate for mysql之-Can't connect to MySQL server on 'localhost'(10038)
1. 卸载navicate for mysql 会留下很多坑,主要是卸载不干净,卸载之后重新安装会出现之前的库内容和库链接还存在的问题,这种情况的出现是卸载残余. 解决办法,清理注册表(网上很多教程但 ...
- 多标签分类的结果评估---macro-average和micro-average介绍
一,多分类的混淆矩阵 多分类混淆矩阵是二分类混淆矩阵的扩展 祭出代码,画线的那两行就是关键啦: 二,查看多分类的评估报告 祭出代码,使用了classicfication_report() 三,宏平均与 ...
- MongoDB 4.0 开发环境搭建集群
环境准备 Liunx 服务器一台 以下示例为单机版安装集群, 没有分片 MongoDB 安装 1.下载 MongoDB tgz 安装包: 可以从下载中心下载: https://www.mongodb. ...
- Android框架式编程之RxJava(一):HelloWorld
Hello World 源码: import android.graphics.Bitmap; import android.graphics.BitmapFactory; import androi ...
- ASP.NET MVC默认配置如有跳转到指定的Area区域中的对应程序中
今天在搭建一个基于MVC的项目,因为项目涉及到了手机和pc端,为了方便和减少二者之间的耦合我在区域(Areas)中建立了两个 程序空间,那么问题来了我想让程序默认跳转到我所指定的areas中的对应项目 ...