惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
博客园 - Franky
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Security Blog
Microsoft Security Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园_首页
腾讯CDC
D
Docker
The Cloudflare Blog
量子位
爱范儿
爱范儿
L
LangChain Blog
博客园 - 三生石上(FineUI控件)
博客园 - 司徒正美
aimingoo的专栏
aimingoo的专栏
Blog — PlanetScale
Blog — PlanetScale
Jina AI
Jina AI
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
博客园 - 聂微东
Vercel News
Vercel News
MyScale Blog
MyScale Blog

博客园 - mshwu

桥接模式(Bridge Pattern) 单例模式 工厂方法模式 命名空间的规划 Download Manager Intent (一) Fragment (一) 2-1 Managing the Activity Lifecycle java1.5新特性(转) 1-4 Starting Another Activity 1-3 Building a Simple User Interface(创建简单的用户界面) 1-2 Android系统架构基本模式解析 1-1 Building Your First Android Application FTP Send File: Source Code Review(转) 动态操作文件 通用日志管理工具 触发器程序 通过CLP实现library之间的自动复制 (转) OVRDBF的用法
Intent(二)
mshwu · 2013-09-07 · via 博客园 - mshwu

以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运行效果如下:

image

选择联系人

image

显示在TextBox上

image

引例子代码:Contact_Picker.rar