Android中统一用Intent来封装程序的“调用意图“。不管程序想启动一个Activity,一个Servicer,还是一个BroadcastReceiver。使用Intent提供了一个统一的编程模型。一定程度上面起到解耦的作用。

1.Intent在Android开发中的作用:

  • Android应用中启动组件(Activity,Servicer,BroadcastReceiver)。
  • 程序组件间的通信。

2.Intent对象的属性:Action(动作),Category(分类),Data(数据),Type(类型),Component(组件),Extra(扩展信息)和Flag(控制旗标)。

3.Intent类型:显示OR隐式。

eg:显示:根据指定的组件创建Intent。Intent intent=new Intent(this,SecondActivity.class);

eg:隐式:根据Intent指定的组件创建Intent。Intent intent=new Intent();intent.setAction(android.intent.action.MAIN);startActivity(intent);

往往在隐式的情况下,我们并不知道启动那个Activity,但是通过配置文件可以知道。在<intent-filter..../>元素中可以包含0~N个<action.../>子元素,0~N个<Category.../>子元素,0~1个<data.../>元素。只要某个配置文件中的某个组件能满足大于或者等于Intent所指定的要求,那么Intent就启动该组件。配置元素<intent-filter.../>中至少包括一个如下的Category子元素。<category android:name="android.intent.category.DEFAULT"/>

下面举一个例子:查看并获取联系人电话。

定义一个main.xml的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
>
<!--显示联系人姓名的文本框-->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/show"
android:editable="false"
android:cursorVisible="false"/>
<!--显示联系人的电话的文本框-->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/phone"
android:editable="false"
android:cursorVisible="false"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查看联系人"
android:id="@+id/bn" /> </LinearLayout>

在mainifest.xml中的定义:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="whushare.cn.whu.getcontactnumber" >
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".SysAction"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.GET_CONTENT"/> <category android:name="android.intent.category.LAUNCHER" />
<data android:mimeType="vnd.android.cursor.item/phone"/>
</intent-filter>
</activity>
</application> </manifest>

Activity文件:

package whushare.cn.whu.getcontactnumber;

import android.app.Activity;

import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; public class SysAction extends Activity{
final int PICK_CONTACT=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
final int PICK_CONTACT=0;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button bn=(Button)findViewById(R.id.bn);
//为bn按钮绑定事件监听器
bn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//创建Intent
Intent intent=new Intent();
//设置Intent的Action属性
intent.setAction(Intent.ACTION_GET_CONTENT);
//设置Intent的Type属性
intent.setType("vnd.android.cursor.item/phone");
//启动Activity,并希望获取改Activity的结果
startActivityForResult(intent,PICK_CONTACT);
}
});
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case(PICK_CONTACT):if(resultCode==Activity.RESULT_OK){
//获取返回的数据
Uri contactData=data.getData();
CursorLoader cursorLoader=new CursorLoader(this,contactData,null,null,null,null);
//查询联系人信息
Cursor cursor=cursorLoader.loadInBackground();
//如果查询到指定的联系人
if(cursor.moveToFirst()){
String contactId=cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
//获取联系人的名字
String name=cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String phoneNumber="此联系人暂未输入电话号码";
//根据联系人查询该联系人的详细信息
Cursor phones=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactId,null,null);
if(phones.moveToFirst()){
//取出电话号码
phoneNumber=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
//关闭游标
phones.close();
EditText show=(EditText)findViewById(R.id.show);
//显示联系人的名称
show.setText(name);
EditText phone=(EditText)findViewById(R.id.phone);
//显示联系人的电话号码
phone.setText(phoneNumber);
}
//关闭游标
cursor.close();
}
break;
}
} }

Intent详解以及实例的更多相关文章

  1. XML参考 :XmlReader 详解、实例

    XML参考 :XmlReader 详解.实例-- 详解 转:http://www.cnblogs.com/Dlonghow/archive/2008/07/28/1252191.html XML参考 ...

  2. Protocol Buffer技术详解(Java实例)

    Protocol Buffer技术详解(Java实例) 该篇Blog和上一篇(C++实例)基本相同,只是面向于我们团队中的Java工程师,毕竟我们项目的前端部分是基于Android开发的,而且我们研发 ...

  3. Protocol Buffer技术详解(C++实例)

    Protocol Buffer技术详解(C++实例) 这篇Blog仍然是以Google的官方文档为主线,代码实例则完全取自于我们正在开发的一个Demo项目,通过前一段时间的尝试,感觉这种结合的方式比较 ...

  4. Java学习-007-Log4J 日志记录配置文件详解及实例源代码

    此文主要讲述在初学 Java 时,常用的 Log4J 日志记录配置文件详解及实例源代码整理.希望能对初学 Java 编程的亲们有所帮助.若有不足之处,敬请大神指正,不胜感激!源代码测试通过日期为:20 ...

  5. groupadd命令详解(实例)

     groupadd命令详解(实例)  1.作用groupadd命令用于将新组加入系统. 2.格式groupadd [-g gid] [-o]] [-r] [-f] groupname 3.主要参数-g ...

  6. GLSL-几何着色器详解跟实例(GS:Geometry Shader)[转]

    [OpenGL4.0]GLSL-几何着色器详解和实例(GS:Geometry Shader) 一.什么是几何着色器(GS:Geometry Shader) Input Assembler(IA)从顶点 ...

  7. CvMat、Mat、IplImage之间的转换详解及实例

    见原博客:http://blog.sina.com.cn/s/blog_74a459380101obhm.html OpenCV学习之CvMat的用法详解及实例 CvMat是OpenCV比较基础的函数 ...

  8. intent详解(一)

    摘录自:http://blog.csdn.net/harvic880925/article/details/38399723 前言:通过重新翻看Android入门书籍,才发现原来自己露掉了那么多基础知 ...

  9. C语言操作WINDOWS系统存储区数字证书相关函数详解及实例

     C语言操作WINDOWS系统存储区数字证书相关函数详解及实例 以下代码使用C++实现遍历存储区证书及使用UI选择一个证书 --使用CertOpenSystemStore打开证书存储区. --在循环中 ...

随机推荐

  1. JavaScript中堆栈解析,已经与delete之间的关系。

    1,在栈中的数据不会随意删除. 2,堆中的数据可以随意删除. 注意:用eval("var a")定义的变量存放在栈中. var 和function 语句在JavaScript中的优 ...

  2. C#中各种数据类型可以表示的数据的范围

    C#中各种数据类型可以表示的数据的范围 BOOL型为int型,一般认为占4个字节,取值TRUE/FALSE/ERROR. sbyte型为有符号8位整数,占1个字节,取值范围在-128 - 127之间. ...

  3. OpenGL核心技术之Gamma校正

    笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,国家专利发明人;已出版书籍:<手把手教你/2.2次幂.Gamma校正后的暗红色就会成为(0.5,0.0 ...

  4. mysql 进阶查询(学习笔记)

    学习笔记,来源:实验楼 ,链接: https://www.shiyanlou.com/courses/9   一.日期计算: 1.要想确定每个宠物有多大,可以使用函数TIMESTAMPDIFF()计算 ...

  5. Windows 10 调节缩略图背景透明度

    A goal is a dream with a deadline. Much effort, much prosperity. 用Windows 10 的朋友都知道 win10 确实挺好用,但是做的 ...

  6. RENOUNCEMENT

    I must not think of thee;and,tired yet syrong,I shun the thought that lurks in all delight--The thou ...

  7. InfiniBand技术和协议架构分析

    Infiniband开放标准技术简化并加速了服务器之间的连接,同时支持服务器与远程存储和网络设备的连接. IB技术的发展 1999年开始起草规格及标准规范,2000年正式发表,但发展速度不及Rapid ...

  8. Windows下下载及安装numpy、pandas及简单应用

    下载numpy 下载地址 https://pypi.python.org/pypi/numpy 进入网站,下载和自己电脑及电脑中安装的python匹配的numpy版本.我的电脑是Win 10 x64位 ...

  9. Robot Framework(一)

    一. 定义 Robot Framework是一款python编写的功能自动化测试框架,具有良好的可扩展性,支持关键字驱动,可以同时测试多种类型的客户端或者接口,可以进行分布式测试执行 二. Robot ...

  10. Java 文件上传中转

    org.apache.commons.httpclient.methods.multipart Class MultipartRequestEntity java.lang.Object org.ap ...