本文简述在Android开发中Intent的常见应用,仅供学习分享使用。

什么是Intent?

  Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。因此,Intent在这里起着一个媒体中介的作用,专门提供组件互相调用的相关信息,实现调用者与被调用者之间的解耦。【官方文档:An intent is an abstract description of an operation to be performed(执行某操作的抽象描述)】

Intent的用途有哪些?

关联不同的组件:

  1. 用来激活启动其他应用程序的组件。
  2. 作为传递数据和事件的桥梁。

Intent调用模式

  1. 显式Intent:直接指定目标组件的ComponentNamae,适合启动同一个应用中的其他组件,比如在某应用程序内,一个Activity启动一个Service。
  2. 隐式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的更多相关文章

  1. 从零开始学android开发-详细谈谈intent的startActivityForResult()方法

    1.两种实现activity跳转的方法 实现activity的跳转主要有两种方法,startActivity()和startActivityForResult();例如activity A跳转到act ...

  2. Android开发学习之路-该怎么学Android(Service和Activity通信为例)

    在大部分地方,比如书本或者学校和培训机构,教学Android的方式都基本类似,就是告诉先上原理方法,然后对着代码讲一下. 但是,这往往不是一个很好的方法,为什么? ① 学生要掌握这个方法的用途,只能通 ...

  3. 一步一步学android控件(之十五) —— DegitalClock & AnalogClock

    原本计划DigitalClock和AnalogClock单独各一篇来写,但是想想,两个控件的作用都一样,就和在一起写一篇了. DegitalClock和AnalogClock控件主要用于显示当前时间信 ...

  4. 一步一步学android控件(之十六)—— CheckBox

    根据使用场景不同,有时候使用系统默认的CheckBox样式就可以了,但是有时候就需要自定义CheckBox的样式.今天主要学习如何自定义CheckBox样式.在CheckBox状态改变时有时需要做一些 ...

  5. 从零開始学android&lt;数据存储(1)SharedPreferences属性文件.三十五.&gt;

    在android中有五种保存数据的方法.各自是: Shared Preferences Store private primitive data in key-value pairs. 相应属性的键值 ...

  6. 一步一步学android控件(之六) —— MultiAutoCompleteTextView

    今天学习的控件是MultiAutoCompleteTextView . 提到MultiAutoCompleteTextView 我们就自然而然地想到AutoCompleteTextView ,就想知道 ...

  7. 从零开始学android -- Service

    废话不多说了,Service是四大组件之一,是一个后台处理长时间运行在主线程不需要依赖ui界面显示的应用组件,切记不能在service中做耗时操作,会阻塞主线程,要做也要在service中开个子线程做 ...

  8. Android笔记---Intent实现Activity跳转

    学了之前的Android控件以及布局,我们就能够做一些UI的设计了,这里我结合之前的知识.以一个小的登录项目来解说下Activity之间跳转. 先看下效果图: 1.登录界面: 2.点击登录按钮跳转到另 ...

  9. 对照 Android 的 Intent 与 iOS StoryBoard 的 Segue - Intent 假设也能添加个prepareForSegue回调就好了

    对照 Android 的 Intent 与 iOS StoryBoard 的 Segue - Intent 假设也能添加个prepareForSegue回调就好了 太阳火神的漂亮人生 (http:// ...

随机推荐

  1. 强化学习(十九) AlphaGo Zero强化学习原理

    在强化学习(十八) 基于模拟的搜索与蒙特卡罗树搜索(MCTS)中,我们讨论了MCTS的原理和在棋类中的基本应用.这里我们在前一节MCTS的基础上,讨论下DeepMind的AlphaGo Zero强化学 ...

  2. windows代码,传入文件名,遍历此目录下所有文件.

    #include <windows.h> #include <vector> using namespace std; BOOL IterAtorFileSaveFile(IN ...

  3. 如何使用JS来开发室内地图商场停车场车位管理系统

    在线体验到室内地图的功能后,手机对室内地图加载一个字,要显示“快”,目前微信和电脑都可以打开室内地图的要求是3秒内打开,能有定位导航的功能最好,这样方便找到要去的地方. 对于经常逛商场的MM来说,哪里 ...

  4. 正则表达式在Java中的使用

    目录 介绍 从简单例子认识正则表达式匹配 Java中对正则表达式的支持(各种语言有相应的实现) 初步认识 . + * ? 范围 认识\s \w \d - 下面介绍数字和字母的正则表达, 这是编程中使用 ...

  5. 前端笔记之CSS(上)

    层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言.CSS不仅可以静态 ...

  6. iOS----------面试常问

    1.valueForKey 和 valueForKeyPath的区别是什么?

  7. DataPipeline联合Confluent Kafka Meetup上海站

    Confluent作为国际数据“流”处理技术领先者,提供实时数据处理解决方案,在市场上拥有大量企业客户,帮助企业轻松访问各类数据.DataPipeline作为国内首家原生支持Kafka解决方案的“iP ...

  8. Ftp修改为主被动模式命令

    FTP是有两种数据连接模式的,主动模式和被动模式. PORT(主动)方式:客户端向服务器的FTP端口(默认是21)发送连接请求,服务器接受连接,建立一条命令链路.当需要传送数据时,客户端在命令链路上用 ...

  9. 积极参与开源项目,促进.NET Core生态社区发展

    今天早上在微信群里聊天聊到百度的SDK 已经支持.NET Core, 百度已经在3月份就支持了,想起当时还是我在他们的github上提的issue: https://github.com/Baidu- ...

  10. 使用Identity Server 4建立Authorization Server (4)

    预备知识: http://www.cnblogs.com/cgzl/p/7746496.html 第一部分: http://www.cnblogs.com/cgzl/p/7780559.html 第二 ...