一起学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:// ...
随机推荐
- 论JVM爆炸的几种姿势及自救方法
前言 如今不管是在面试还是在我们的工作中,OOM总是不断的出现在我们的视野中,所以我们有必要去了解一下导致OOM的原因以及一些基本的调整方法,大家可以通过下面的事例来了解一下什么样的代码会导致OOM, ...
- 在已有的Asp.net MVC项目中引入Taurus.MVC
Taurus.MVC是一个优秀的框架,如果要应用到已有的Asp.net MVC项目中,需要修改一下. 1.前提约定: 走Taurus.MVC必须指定后缀.如.api 2.原项目修改如下: web.co ...
- 新手学习WEB前端流程以及学习中常见的误区
学习web前端编程技术肯定是以就业拿到高薪工作为主要目的的,可是高薪不会那么轻易拿到,这是一个最简单的道理.没有付出就没有回报,在整个学习web前端编程技术的过程中,你需要付出时间.精力.金钱.废话不 ...
- jquery.rotate.js实现可选抽奖次数和中奖内容的转盘抽奖代码
需求: 抽奖代码最多可以抽奖5次,而且,每次只会中“2000元理财金”或者“谢谢参与”,其它的不会抽中(哈哈,果然都是套路). 效果如下: 一.页面结构: ? 1 2 3 4 5 6 7 8 9 10 ...
- SAP MM已经转成PO的采购申请Item依旧可以被删除?
SAP MM已经转成PO的采购申请Item依旧可以被删除? 笔者测试发现,我们可以删除已产生PO的PR item, 系统只是给一个警告信息,不阻止保存. Purchase orders already ...
- C# XML序列化/反序列化参考
.NET提供了很不错的XML序列化/反序列化器,(它们所在的命名空间为System.Xml.Serialization)这是很方便的,下面对它的使用做一些总结,以供参考. 1,简单序列化 public ...
- SQL注入攻击的常见方式及测试方法
本文主要针对SQL注入的含义.以及如何进行SQL注入和如何预防SQL注入让小伙伴有个了解.适用的人群主要是测试人员,了解如何进行SQL注入,可以帮助我们测试登录.发布等模块的SQL攻击漏洞,至于如何预 ...
- Java核心技术第五章——1.类、超类、子类(1)
1.定义子类: 关键字extends表明正在构造的新类派生与一个已存在的类.已存在的类称为超类.基类或父类:新类称为子类.派生类或孩子类.超类和子类是Java程序员最常用的两个术语(emmm~~我觉得 ...
- MySQL 查询出的时间相差几个小时
最近做的一个springboot2.0项目. 前提是我的服务器时区没有问题: [root@wangbo ~]# date -R Mon, 22 Apr 2019 19:24:33 +0800 可以参考 ...
- 跟我一起学opencv 第一课之图像加载,修改,保存
使用opencv前记得引入库和头文件: #include<opencv2\opencv.hpp> 1.加载图像(cv::imread)(OPENCV 支持 JPG,PNG,TIFF等常见格 ...