Android使用AsyncQueryHandler實(shí)現(xiàn)獲取手機(jī)聯(lián)系人功能
利用AsyncQueryHandler能異步任務(wù)獲取手機(jī)聯(lián)系人,增加用戶(hù)體驗(yàn),使用起來(lái)也很方便。不多說(shuō),上干貨。
布局文件main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:ignore="HardcodedText" >
<Button
android:id="@+id/bt_getCantacts"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="獲取聯(lián)系人信息" />
</LinearLayout>
contact.java 聯(lián)系人bean文件
package com.larson.cantact;
public class Contact {
private int contactId;//聯(lián)系人ID
private String displayName;//聯(lián)系人姓名
private String phoneNum;//聯(lián)系人手機(jī)號(hào)
private String sortKey;//排序Key
private Long photoId;//頭像ID
private String lookUpKey;
private int selected = 0;//被選中的行號(hào)
private String formattedNumber;//被格式化的號(hào)碼
private String pinyin;//姓名對(duì)應(yīng)的漢語(yǔ)拼音
public int getContactId() {
return contactId;
}
public void setContactId(int contactId) {
this.contactId = contactId;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public String getSortKey() {
return sortKey;
}
public void setSortKey(String sortKey) {
this.sortKey = sortKey;
}
public Long getPhotoId() {
return photoId;
}
public void setPhotoId(Long photoId) {
this.photoId = photoId;
}
public String getLookUpKey() {
return lookUpKey;
}
public void setLookUpKey(String lookUpKey) {
this.lookUpKey = lookUpKey;
}
public int getSelected() {
return selected;
}
public void setSelected(int selected) {
this.selected = selected;
}
public String getFormattedNumber() {
return formattedNumber;
}
public void setFormattedNumber(String formattedNumber) {
this.formattedNumber = formattedNumber;
}
public String getPinyin() {
return pinyin;
}
public void setPinyin(String pinyin) {
this.pinyin = pinyin;
}
}
MainActivity.java
package com.larson.cantact;
import java.util.ArrayList;
import android.app.Activity;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button myCantacts;
private AsyncQueryHandler asyncQuery;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myCantacts = (Button) this.findViewById(R.id.bt_getCantacts);
myCantacts.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
asyncQuery = new MyAsyncQueryHandler(getContentResolver());
initSQL();
}
});
}
protected void initSQL() {
// 聯(lián)系人URI
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
// 聯(lián)系人ID,聯(lián)系人NAME,
String[] projection = { ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.DATA1, "sort_key",
ContactsContract.CommonDataKinds.Phone.CONTACT_ID, };
asyncQuery.startQuery(0, null, uri, projection, null, null,
"sort_key COLLATE LOCALIZED asc");
}
private class MyAsyncQueryHandler extends AsyncQueryHandler {
public MyAsyncQueryHandler(ContentResolver cr) {
super(cr);
}
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
querying(cursor);
}
}
private void querying(final Cursor cursor) {
Handler handlerInsertOrder = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case MyAsyncTask.DOWNLOADING_START_MESSAGE:
System.out.println("begin to sort out t9");
break;
case MyAsyncTask.DOWNLOAD_END_MESSAGE:
Bundle bundle1 = msg.getData();
ArrayList<ContactBean> list = (ArrayList<ContactBean>) bundle1
.get("完成");
for (ContactBean ci : list) {
System.out.println(ci.getDisplayName());
System.out.println(ci.getPhoneNum());
System.out.println("--------------------------------");
}
break;
default:
break;
}
super.handleMessage(msg);
}
};
MyAsyncTask.startRequestServerData(this, handlerInsertOrder, cursor);
}
}
自定義的MyAsyncTask.java
package com.anjoyo.cantact;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
public class MyAsyncTask extends
AsyncTask<Cursor, Void, ArrayList<ContactBean>> {
/** 開(kāi)始整理 */
public static final int DOWNLOADING_START_MESSAGE = 7;
/** 整理結(jié)束 */
public static final int DOWNLOAD_END_MESSAGE = 17;
private Context mContext = null;
private Handler mHandler = null;
protected MyAsyncTask(Context context, Handler handler) {
this.mContext = context;
this.mHandler = handler;
}
@Override
protected void onPreExecute() {
sendStartMessage(DOWNLOADING_START_MESSAGE);
}
@Override
protected ArrayList<ContactBean> doInCursor... params) {
// 只需要把原來(lái)放在子線程的代碼放到這個(gè)方法就行
Cursor cursor = params[0];
ArrayList<ContactBean> ciList = new ArrayList<ContactBean>();
if (cursor != null && cursor.getCount() > 0) {
try {
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
cursor.moveToPosition(i);
String name = cursor.getString(1);
String number = cursor.getString(2);
int contactId = cursor.getInt(4);
ContactBean contactInfo = new ContactBean();
contactInfo.setContactId(contactId);
contactInfo.setPhoneNum(number);
contactInfo.setDisplayName(name);
if (contactInfo.getDisplayName() == null) {
contactInfo.setDisplayName(contactInfo.getPhoneNum());
}
ciList.add(contactInfo);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return ciList;
}
@Override
protected void onPostExecute(ArrayList<ContactBean> result) {
sendEndMessage(DOWNLOAD_END_MESSAGE, result);
}
public static void startRequestServerData(Context context, Handler handler,
Cursor cursor) {
new MyAsyncTask(context, handler).execute(cursor);
}
/**
* 發(fā)送開(kāi)始整理消息
*
* @param messageWhat
*/
private void sendStartMessage(int messageWhat) {
Message message = new Message();
message.what = messageWhat;
if (mHandler != null) {
mHandler.sendMessage(message);
}
}
/**
* 發(fā)送整理結(jié)束消息
*
* @param messageWhat
*/
private void sendEndMessage(int messageWhat, ArrayList<ContactBean> result) {
Message message = new Message();
message.what = messageWhat;
Bundle bundle = new Bundle();
bundle.putSerializable("完成", result);
message.setData(bundle);
if (mHandler != null) {
mHandler.sendMessage(message);
}
}
}
小工具,供人參考,方便廣大程序員,歡迎指正。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android ContentProvider實(shí)現(xiàn)手機(jī)聯(lián)系人讀取和插入
- Android讀取手機(jī)通訊錄聯(lián)系人到自己項(xiàng)目
- android仿微信聯(lián)系人索引列表功能
- Android保存聯(lián)系人到通訊錄的方法
- android如何獲取聯(lián)系人所有信息
- Android ContentProvider實(shí)現(xiàn)獲取手機(jī)聯(lián)系人功能
- Android 獲取手機(jī)聯(lián)系人實(shí)例代碼詳解
- android實(shí)現(xiàn)讀取、搜索聯(lián)系人的代碼
- Android ContentProvider獲取手機(jī)聯(lián)系人實(shí)例
- Android小程序?qū)崿F(xiàn)訪問(wèn)聯(lián)系人
相關(guān)文章
Android onNewIntent()觸發(fā)機(jī)制及注意事項(xiàng)
這篇文章主要介紹了Android onNewIntent()觸發(fā)機(jī)制及注意事項(xiàng)的相關(guān)資料,需要的朋友可以參考下2016-09-09
Android中給按鈕同時(shí)設(shè)置背景和圓角示例代碼
相信每位Android開(kāi)發(fā)者們都遇到過(guò)給按鈕設(shè)置背景或者設(shè)置圓角的需求,但是如果要同時(shí)設(shè)置背景和圓角該怎么操作才是方便快捷的呢?這篇文章通過(guò)示例代碼給大家演示了Android中給按鈕同時(shí)設(shè)置背景和圓角的方法,有需要的朋友們可以參考借鑒。2016-10-10
android中ListView數(shù)據(jù)刷新時(shí)的同步方法
這篇文章主要介紹了android中ListView數(shù)據(jù)刷新時(shí)的同步方法,涉及Android刷新listview實(shí)現(xiàn)數(shù)據(jù)同步的技巧,需要的朋友可以參考下2015-05-05
Android數(shù)據(jù)傳輸中的參數(shù)加密代碼示例
這篇文章主要介紹了Android數(shù)據(jù)傳輸中的參數(shù)加密代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
Android中播放Gif動(dòng)畫(huà)取巧的辦法
本文給大家介紹Android中播放Gif動(dòng)畫(huà)取巧的辦法的相關(guān)資料,涉及到android 播放gif動(dòng)畫(huà)相關(guān)知識(shí),感興趣的朋友一起學(xué)習(xí)吧2016-03-03
android實(shí)現(xiàn)底部導(dǎo)航欄
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)底部導(dǎo)航欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06

