Intent详解以及实例
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详解以及实例的更多相关文章
- XML参考 :XmlReader 详解、实例
XML参考 :XmlReader 详解.实例-- 详解 转:http://www.cnblogs.com/Dlonghow/archive/2008/07/28/1252191.html XML参考 ...
- Protocol Buffer技术详解(Java实例)
Protocol Buffer技术详解(Java实例) 该篇Blog和上一篇(C++实例)基本相同,只是面向于我们团队中的Java工程师,毕竟我们项目的前端部分是基于Android开发的,而且我们研发 ...
- Protocol Buffer技术详解(C++实例)
Protocol Buffer技术详解(C++实例) 这篇Blog仍然是以Google的官方文档为主线,代码实例则完全取自于我们正在开发的一个Demo项目,通过前一段时间的尝试,感觉这种结合的方式比较 ...
- Java学习-007-Log4J 日志记录配置文件详解及实例源代码
此文主要讲述在初学 Java 时,常用的 Log4J 日志记录配置文件详解及实例源代码整理.希望能对初学 Java 编程的亲们有所帮助.若有不足之处,敬请大神指正,不胜感激!源代码测试通过日期为:20 ...
- groupadd命令详解(实例)
groupadd命令详解(实例) 1.作用groupadd命令用于将新组加入系统. 2.格式groupadd [-g gid] [-o]] [-r] [-f] groupname 3.主要参数-g ...
- GLSL-几何着色器详解跟实例(GS:Geometry Shader)[转]
[OpenGL4.0]GLSL-几何着色器详解和实例(GS:Geometry Shader) 一.什么是几何着色器(GS:Geometry Shader) Input Assembler(IA)从顶点 ...
- CvMat、Mat、IplImage之间的转换详解及实例
见原博客:http://blog.sina.com.cn/s/blog_74a459380101obhm.html OpenCV学习之CvMat的用法详解及实例 CvMat是OpenCV比较基础的函数 ...
- intent详解(一)
摘录自:http://blog.csdn.net/harvic880925/article/details/38399723 前言:通过重新翻看Android入门书籍,才发现原来自己露掉了那么多基础知 ...
- C语言操作WINDOWS系统存储区数字证书相关函数详解及实例
C语言操作WINDOWS系统存储区数字证书相关函数详解及实例 以下代码使用C++实现遍历存储区证书及使用UI选择一个证书 --使用CertOpenSystemStore打开证书存储区. --在循环中 ...
随机推荐
- JMeter接口测试和压力测试
JMeter接口测试和压力测试 JMeter可以做接口测试和压力测试.其中接口测试的简单操作包括做http脚本(发get/post请求.加cookie.加header.加权限认证.上传文件).做web ...
- Shell 实现找出两个目录下的同名文件方法
# 首先我们来创建一些 2 个目录,里面的目录结构及相关文件如下所示: # 从上面的测试目录可以看到, lol.txt lol2.txt 两个文件是两个目录下的同名文件 # 有实际例子,思路就容易出来 ...
- 课堂测试Mysort
课上没有做出来的原因 因为自己平时很少动手敲代码,所以在自己写代码的时候往往会比较慢,而且容易出现一些低级错误,再加上基础没有打牢,对于老师课上所讲的知识不能及时的理解消化,所以可能以后的课上测试都要 ...
- linux下字典生成工具-crunch与rtgen
所谓的密码字典主要是配合密码破解软件所使用,密码字典里包括许多人们习惯性设置的密码.这样可以提高密码破解软件的密码破解成功率和命中率,缩短密码破解的时间.当然,如果一个人密码设置没有规律或很复杂,未包 ...
- Introspector内省和反射的区别.
Introspector 是一个专门处理bean的工具类.用来获取Bean体系里的 propertiesDescriptor,methodDescriptor. 要理解这个,就要理解下面几个议题. ...
- [BZOJ1823]满汉全席
Description 满汉全席是中国最丰盛的宴客菜肴,有许多种不同的材料透过满族或是汉族的料理方式,呈现在數量繁多的菜色之中.由于菜色众多而繁杂,只有极少數博学多闻技艺高超的厨师能够做出满汉全席,而 ...
- git代码提交与克隆
在工作中,越来越多的人会使用git来管理代码.下面简单的介绍一下git在工作中的使用流程 1.给你一个git地址,将代码拉下来基本操作流程如下: 1.1 git clone "项目地址&qu ...
- spark启动
注意在启动spark时候要指定参数 要不就死启动的单机版的 /usr/local/spark-1.5.2-bin-hadoop2.6/bin/spark-shell \ --master spark: ...
- golang learning
开发用py和go 入职前学了几天py入职后看的代码也是py 现在终于还是要学go了 初体验:感觉和c py都很像 入门语法看起来很简单的样子 学了py之后现在各种随意 需要多注意 在函数传参的时候 c ...
- jQuery EasyUI Datagrid VirtualScrollView视图简单分析
大家都知道EasyUI的Datagrid组件在加载大数据量时的优势并不是很明显,相对于其他一些框架,如果数据量达到几千,便会比较慢,特别是在IE下面.针对这种情况,我们首要做的是要相办法优化datag ...