一起学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:// ...
随机推荐
- JavaScript实现登录窗口的拖拽
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 从PRISM开始学WPF,Prism7更新了什么
当时我在搬运Prism6.3的sample代码的时候,就是因为网上的资料太老旧,万万没想到这给自己挖了一个坑,因为我在做笔记的时候,prism已经在更新7.0了 现在已经是7.2了,(lll¬ω¬), ...
- sun.misc jar包
一直以来Base64算法的加密解密都是使用sun.misc包下的BASE64Encoder及BASE64Decoder来进行的.但是这个类是sun公司的内部方法,并没有在Java API中公开过,不属 ...
- C语言超级搞笑的代码,冷笑话我们程序员也会讲的啊!
百年修得足下点击本文 欢迎来到"C语言基础"专题,今天我们放松一天,不学习知识,来看下大千世界的千奇百怪的C语言代码,你见过那些? 1.关于随机数这回事 这个随机数有点意思哦. 2 ...
- ES6数组扩展运算符
1 扩展运算符的运用 (1)复制数组 数组是复合的数据类型,直接复制的话,只是复制了指向底层数据机构的指针,而不是克隆一个全新的数组; const a1=[1,2]; const a2= a1; a2 ...
- July 04th. 2018, Week 27th. Wednesday
And if you really want to see what people are, all you have to do is to look. 想真正了解他人,只需要用心看. From W ...
- CYQ.Data 支持 PostgreSQL 数据库
前言: 很久之前,就有同学问我CYQ.Data能不能支持下PostgreSQL,之后小做了下调查,发现这个数据库用的人少,加上各种因素,就一直没动手. 前两天,不小心看了一下Github上的消息: 看 ...
- Java Main参数解析(Args4j)
最近实现一个工具,Main函数会有很多参数,而且参数类型不同,为了统一解析,网上找到三方工具类Args4j,轻松搞定. 代码实例如下: 定义解析类: import java.io.File impor ...
- 第1章 程序设计和C语言
1.1什么是计算机程序 程序,就是一组计算机能识别和执行的指令.每一条指令使计算机执行特定的操作.只要让计算机执行这个程序,计算机就会“自动地”执行各条指令,有条不紊地进行工作. 1.2什么是计算机语 ...
- 1.Flask URL和视图
1.1.第一个flask程序 from flask import Flask #创建一个Flask对象,传递__name__参数进去 app = Flask(__name__) #url与视图映射 @ ...