本文简述在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. MVC设计模式思想及简单实现

    一.什么是MVC MVC即Model-View-Controller(模型-视图-控制器)是一种软件设计模式,最早出现在Smalltalk语言中,后被Sun公司推荐为Java EE平台的设计模式. M ...

  2. 什么是移动BI

    移动商务智能(移动商务智能或移动智能)定义为一个包含技术和组织元素的系统,可向用户提供历史和/或实时信息,以便在智能手机和平板电脑等移动设备上进行分析笔记本电脑),以实现有效的决策和管理支持,以提高公 ...

  3. MySQL和SQLyog的配置-安装及遇到的问题

    1. 安装MySQL 我先是参考了菜鸟教程上的#Windows 上安装 MySQL #版块的安装教程,不过经历了种种磨难,我最后是按照#此篇博客#安装成功的. 这两篇教程最大的差别是在于下载的版本不同 ...

  4. Packets larger than max_allowed_packet are not allowed(mysql数据查询提示:不允许超过允许的最大数据包)解决方案

    问题背景: 首先我的项目运用的时Mysql,在做一个数据更新操作的时候,提示异常:System.Exception:“ExecuteNonQuery:Packets larger than max_a ...

  5. python脚本简化jar操作命令

    本篇和大家分享的是使用python简化对jar包操作命令,封装成简短关键字或词,达到操作简便的目的.最近在回顾和构思shell脚本工具,后面一些文章应该会分享shell内容,希望大家继续关注. 获取磁 ...

  6. 如何在ASP.NET Core程序启动时运行异步任务(2)

    原文:Running async tasks on app startup in ASP.NET Core (Part 2) 作者:Andrew Lock 译者:Lamond Lu 在我的上一篇博客中 ...

  7. java~lombok里的Builder注解

    lombok注解在java进行编译时进行代码的构建,对于java对象的创建工作它可以更优雅,不需要写多余的重复的代码,这对于JAVA开发人员是很重要的,在出现lombok之后,对象的创建工作更提供Bu ...

  8. JavaScript夯实基础系列(三):this

      在JavaScript中,函数的每次调用都会拥有一个执行上下文,通过this关键字指向该上下文.函数中的代码在函数定义时不会执行,只有在函数被调用时才执行.函数调用的方式有四种:作为函数调用.作为 ...

  9. MySQL高可用复制管理工具 —— Orchestrator使用

    背景 在上一篇「MySQL高可用复制管理工具 —— Orchestrator介绍」中大致介绍了Orchestrator的功能.配置和部署,当然最详细的说明可以查阅官方文档.本文开始对Orchestra ...

  10. 贝塞尔曲线控件 for .NET (EN)

    Conmajia 2012 Updated on Feb. 18, 2018 In Photoshop, there is a very powerful feature Curve Adjust, ...