Android使用AsyncQueryHandler实现获取手机联系人功能

Android使用AsyncQueryHandler实现获取手机联系人功能,第1张

概述利用AsyncQueryHandler能异步任务获取手机联系人,增加用户体验,使用起来也很方便。不多说,上干货。

利用AsyncqueryHandler能异步任务获取手机联系人,增加用户体验,使用起来也很方便。不多说,上干货。

布局文件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="获取联系人信息" /> </linearLayout> 

contact.java 联系人bean文件

package com.larson.cantact; public class Contact {   private int contactID;//联系人ID   private String displayname;//联系人姓名   private String phoneNum;//联系人手机号   private String sortKey;//排序Key   private Long photoID;//头像ID   private String lookUpKey;   private int selected = 0;//被选中的行号   private String formattednumber;//被格式化的号码   private String pinyin;//姓名对应的汉语拼音                                                                                                 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() {     // 联系人URI     Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;     // 联系人ID,联系人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,"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>> {   /** 开始整理 */   public static final int DOWNLOADING_START_MESSAGE = 7;   /** 整理结束 */   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) {     // 只需要把原来放在子线程的代码放到这个方法就行     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);   }   /**    * 发送开始整理消息    *    * @param messageWhat    */   private voID sendStartMessage(int messageWhat) {     Message message = new Message();     message.what = messageWhat;     if (mHandler != null) {       mHandler.sendMessage(message);     }   }   /**    * 发送整理结束消息    *    * @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);     }   } } 

 小工具,供人参考,方便广大程序员,欢迎指正。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android使用AsyncQueryHandler实现获取手机联系人功能全部内容,希望文章能够帮你解决Android使用AsyncQueryHandler实现获取手机联系人功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1145550.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存