Intent(二)
以Android高级编程一书中的一个例子为例:
1, 创建一个ContactPicker项目,其中包含一个ContactPicker Activity
package com.paad.contactpicker; import android.app.Activity;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter; public class ContactPicker extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main); //创建一个新的Cursor来遍历存储在联系人列表中的联系人,并使用SimpleCursorArrayAdapter把它绑定到List View上,更好的做法应该使用Cursor Loader
final Cursor c = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null); String[] from = new String[] { Contacts.DISPLAY_NAME_PRIMARY };
int[] to = new int[] { R.id.itemTextView }; SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.listitemlayout,c,from,to);
ListView lv = (ListView)findViewById(R.id.contactListView);
lv.setAdapter(adapter); //返回选择的联系人信息给调用的Activity
lv.setOnItemClickListener(new ListView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id) {
// Move the cursor to the selected item
c.moveToPosition(pos);
// Extract the row id.
int rowId = c.getInt(c.getColumnIndexOrThrow("_id"));
// Construct the result URI.
Uri outURI = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, rowId);
Intent outData = new Intent();
outData.setData(outURI);
setResult(Activity.RESULT_OK, outData);
finish();
}
}); }
}
2,修改main.xml布局资源来包含一个ListView控件,后面将使用这个控件显示联系人
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:id="@+id/contactListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
3, 创建一个新的包含一个单独的TextView控件的listitemlayout.xml布局资源,它将用来在ListView中显示每一个联系人
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/itemTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16dp"
android:textColor="#FFF"
/>
</LinearLayout>
4, 修改应用程序的manifest文件,并更新Activity的intent-filter标签以添加在联系人数据上对action_pick动作的支持
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.paad.contactpicker">
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<application android:icon="@drawable/ic_launcher">
<activity android:name=".ContactPicker" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.PICK"></action> <category android:name="android.intent.category.DEFAULT"></category> <data android:path="contacts" android:scheme="content"></data>
</intent-filter>
</activity>
</application>
</manifest>
到此子Activity完成,下面创建一个调用此子Activity的Activity
package com.paad.contactpicker; import android.app.Activity;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; public class ContactPickerTester extends Activity { public static final int PICK_CONTACT = 1; //隐式调用联系人列表Activity
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contactpickertester); Button button = (Button)findViewById(R.id.pick_contact_button); button.setOnClickListener(new OnClickListener() {
public void onClick(View _view) {
Intent intent = new Intent(Intent.ACTION_PICK,
Uri.parse("content://contacts/"));
startActivityForResult(intent, PICK_CONTACT);
}
});
}
//处理从子Activity返回的数据
@Override
public void onActivityResult(int reqCode, int resCode, Intent data) {
super.onActivityResult(reqCode, resCode, data); switch(reqCode) {
case (PICK_CONTACT) : {
if (resCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
c.moveToFirst();
String name = c.getString(c.getColumnIndexOrThrow(
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));
c.close();
TextView tv = (TextView)findViewById(R.id.selected_contact_textview);
tv.setText(name);
}
break;
}
default: break;
}
} }
对应的布局文件包括一个按钮和一个用来显示用户选择的Textbox
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/selected_contact_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/pick_contact_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pick Contact"
/>
</LinearLayout>
些Demo运行效果如下:

选择联系人

显示在TextBox上

引例子代码:Contact_Picker.rar
Intent(二)的更多相关文章
- Intent(二)隐式调用intent
在上一节我们一起学习了显示调用Intent,这一节我们来学习如何隐式调用Ingtent.有了这个我们就可以调用其他的线程,或者程序,可以让我们的应用程序变得多彩,如打开网页,拨打电话等. 接下来让我们 ...
- Android学习笔记Intent二
上篇随笔大概写了了Intent学习的大纲,这篇通过代码理解下Intent的ComponentName属性的使用 ComponentName,中文意思是组件名称,通过Intent的setsetCompo ...
- 二、activity与Intent
(一) 多个activity之间的跳转(无值传递) 第一步:创建activity(其实就是jave文件),并进行注册 在AndroidManifest.xml中 <activity androi ...
- Intent启动一个新的页面
一,Intent(目的) 的分类 显式 Intent 构造函数重载之一: Intent intent = new Intent(FirstActivity.this,SecondActivity.cl ...
- Intent七大属性
一.Intent的作用是什么? 1.Intent 用于封装程序的”调用意图“.两个Activity之间,可以把需要交换的数据封装成Bundle对象,然后使用Intent携带Bundle对象,实现 ...
- Intent (一)
1,简介 Intent 是一种消息传递机制,可以理解为一种对消息的封装,执行某操作的抽象描述,可用于应用程序内部及应用程序之间 其组成包括: 要执行的动作(action) 如VIEW_ACTION(查 ...
- Intent是什么?
一.理解Intent: 在一个Android应用中,主要是由四种组件组成的,这四种组件可参考“Android应用的构成”.而这四种组件是独立的,它们之间可以互相调用,协调工作,最终组成一个真正的And ...
- 使用Intent实现Activity的显式跳转
[正文] 这里以按钮实现活动跳转为例,为实现这个功能,我们需要三个步骤: 1.点击按钮才发生页面跳转,因此,第一步我们先要找到要点击的按钮 如何拿到按钮对象呢?通过资源id,前面我们提到过,在R.id ...
- Android菜鸟的成长笔记(8)——Intent与Intent Filter(上)
原文:[置顶] Android菜鸟的成长笔记(8)——Intent与Intent Filter(上) Intent代表了Android应用的启动“意图”,Android应用将会根据Intent来启动指 ...
随机推荐
- 《DNS的正向反向解析》RHEL6
DNS的正向解析: Iptables –F Setenforce 0 安装DNS服务器的软件包: 启动DNS服务器: 修改DNS的配置文件:vim /etc/named.conf 修改DNS的配置:( ...
- [视频]MAC中如何单独放大文本字体
我们知道使用MAC触控板的双指合拢手势可以进行放大或缩小操作,但其对应的是整个界面内容的放大及缩小,如果仅对其文本内容进行放大或缩小,可使用快捷键进行操作. 默认的 ”Command” + “=“ ...
- mac OS X下git代码行统计命令
1.统计某人的代码提交量,包括增加,删除 git log --author=-- --until=-- --pretty=tformat: --numstat | awk '{ add += $1 ; ...
- Delphi XE5教程8:使用Delphi命名空间
// Project file declarations... //项目文件声明… program MyCompany.ProjectX.ProgramY; // Unit source file d ...
- R语言基础(一) 可视化基础
##数据获取 x1=round(runif(100,min=80,max=100)) x2=round(rnorm(100,mean=80, sd=7)) x3=round(rnorm(100,mea ...
- 菜鸟学习Struts——简易计算器
这是学习Struts的一个简单的例子文件结构如下: 1.配置Struts环境 2.新建input.jsp,success.jsp,error.jsp input.jsp代码如下: <%@ pag ...
- Nginx模块开发-理解HTTP配置
理解HTTP配置 相关数据结构 先明白Nginx下述数据结构,再理解 HTTP配置的解析与合并过程 ngx_module_t 官方API typedef struct{ NGX_MODULE_V1; ...
- C#的winform小合集
C#的winform小合集 博主很懒,又想记录一下自己的所做所为,仅此而已,供自己日后所看.这个是博主自主学习C#所写的一些小程序,有好玩的,也有一些无聊闲得蛋疼所作的. 内容介绍 C#入门窗口输出h ...
- 0x03伪指令
等号伪指令 = 相当于指定常量,由等号定义的符号常量不占用存储空间. count = 1234 可以重复定义多次,EQU则不容许 EQU伪指令 1.常量名 EQU 表达式 NUMBER EQU 10* ...
- 在Windows下忘记MySQL最高用户权限密码的解决方案
1.打开MySQL配置文件 my.ini中,添加上skip-grant-tables,可以添加到文件的末尾或者是这添加到[mysqld]的下面(直接添加在my.ini文件最后亲测可以,但是在[mysq ...