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. kali 2016:mount ntfs 分区只读 --Falling back to read-only mount because the NTFS partition is in an unsafe state.

    mount ntfs 分区 mount /dev/sdb1 /mnt/d 提示: The disk contains an unclean file system (0, 0).Metadata ke ...

  2. API接口幂等性框架设计

    表单重复提价问题 rpc远程调用时候 发生网络延迟  可能有重试机制 MQ消费者幂等(保证唯一)一样 解决方案: token 令牌 保证唯一的并且是临时的  过一段时间失效 分布式: redis+to ...

  3. Nodejs WEB开发常用库和框架

    我在Nodejs的体系里也算泡了很久了,的确非常喜欢javascript和Nodejs. 在我看来,用nodejs做web开发有以下几个优点: Javascript作为一个语法异常简单的脚本语言,约束 ...

  4. java中TreeMap集合的常用方法

    实现Map集合的方法这里就不在讲了 https://www.cnblogs.com/xiaostudy/p/9510763.html public Map.Entry<K,V> ceili ...

  5. DelayQueue与ProirityBlockingQueue

    DelayQueue是一个无界队列,只有在延迟期满的时候,才可以取出元素.该队列的头部存储的延期期满了后保存时间最长的元素. DelayQueue阻塞队列在我们系统开发中也常常会用到,例如:缓存系统的 ...

  6. PermutationSequence,求第k个全排列

    问题描述:给定一个数组,数组里面元素不重复,求第k个全排列. 算法分析:这道题就是用到取商取模运算. public String getPermutation(int n, int k) { // i ...

  7. http客户端-基于boost开发

    http客户端-基于boost开发 基于BOOST编写的http客户端,作为BOOST开发学习之用.目前支持功能: http协议,单向链接返回http response code 200 可conte ...

  8. JNI_Z_10_Java的数组

    在Java中数组分为两种: (1).基本类型数组 (2).对象类型(Object[])的数组 (数组中存放的是指向Java对象中的引用) 一个能通用于两种不同类型数组的函数: GetArrayLeng ...

  9. Ajax-02 iframe实现伪“Ajax”

    需求: 用户输入URL,使用iframe将目标URL的内容加载到页面指定位置(局部刷新) <!DOCTYPE html> <html lang="en"> ...

  10. WSL安装xfce4

    参考:https://github.com/Microsoft/WSL/issues/637 安装组件 1. win10 上安装 Xming https://sourceforge.net/proje ...