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打开证书存储区. --在循环中 ...
随机推荐
- Linux内核参数之rp_filter
一.rp_filter参数介绍 rp_filter参数用于控制系统是否开启对数据包源地址的校验. 首先看一下Linux内核文档documentation/networking/ip-sysctl.tx ...
- Linux防火墙--iptables学习
iptables是Linux系统提供的一个强大的防火墙工具,可以实现包过滤.包重定向.NAT转换等功能.iptables是免费的,iptables是一个工具,实际的功能是通过netfilter模块来实 ...
- sublime批量替换文本重复单词
事先需要把单词打到文本的每一行 排序 按F9或者选择菜单:Edit > Sort Lines,对每行文本进行排序 查找重复行 排序好后,按Ctrl+F,调出查找面板 查找字符串: ^(.+)$[ ...
- Linux系统中使用netcat命令的奇技淫巧
netcat是网络工具中的瑞士军刀,它能通过TCP和UDP在网络中读写数据.通过与其他工具结合和重定向,你可以在脚本中以多种方式使用它.使用netcat命令所能完成的事情令人惊讶. netcat所做的 ...
- quartz(5)--作业管理和存储
作业一旦被调度,调度器需要记住并且跟踪作业和它们的执行次数.如果你的作业是30分钟后或每30秒调用,这不是很有用.事实上,作业执行需要非常准确和即时调用在被调度作业上的execute()方法.Quar ...
- 电脑 HOST 文件
路径: C:\Windows\System32\drivers\etc\HOSTS
- MVC viewbag & viewdata
弱类型:ViewData[""] 动态型:ViewBag dynamic ViewData 是字典型的(Dictionary),ViewBag 不再是字典的键值对结构,而是dyna ...
- jQ&js给label
<strong>当前角色:</strong><label id="lblRoleName" style="margin-bottom: 0p ...
- 一个问题:C#引用类型传参,说出你的答案
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { People p = new Peo ...
- scala学习手记37 - 容器的使用
这次统一看一下scala中容器类的几个方法. Set filter()方法 filter()方法用来从Set中过滤获取含有指定特征的元素.示例代码如下: val colors1 = Set(" ...