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. SET 语句积累

    SET IDENTITY_INSERT 表名字 off 注解: 把值插入到自动编号(或者说是标识列,IDENTITY)中去,需要设定 SET IDENTITY_INSERT 语法:SET IDENTI ...

  2. Android LCD

    Android LCD(一):LCD基本原理篇Android LCD(二):LCD常用接口原理篇Android LCD(三):Samsung LCD接口篇Android LCD(四):LCD驱动调试篇

  3. 一步一步教你读懂NET中IL

    .NET CLR 和 Java VM 都是堆叠式虚拟机器(Stack-Based VM),也就是说,它们的指令集(Instruction Set)都是采用堆叠运算的方式:执行时的资料都是先放在堆叠中, ...

  4. 基于SSM的单点登陆05

    springmvc.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...

  5. C# ---sender

    在某个方法中: 第一种写法: private void btn4_Click_1(object sender, RoutedEventArgs e) { btn1_Click(null, null); ...

  6. 20145109 实验二 Java面向对象程序设计

    实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 S.O.L.I.D原则: ...

  7. Spark 实现自定义对象sequenceFile方式存储,读写示例(scala编写)

    package com.fuge.bigdata.datahub.analysis import java.io.{DataInput, DataOutput} import com.fuge.big ...

  8. Spring-boot CLI下载

    Spring-boot CLI下载地址: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#getting-s ...

  9. Linux 通过进程Pid与端口互查

    ps -aux 状态详解 https://blog.csdn.net/whatday/article/details/54409387. linux下通过进程名查看其占用端口: https://www ...

  10. activity状态的保存和恢复

    activity状态的保存和恢复 一.简介 1.保存activity状态 * 保存activity状态,onSaveInstanceState这个方法会自动保存有ID的组件的状态 * 没有ID的组件或 ...