我正在努力从我的应用程序中获取电话簿和SIM卡的所有联系人.我想将所有联系人存储在我的应用程序sqlite DB中.我正在使用的代码在正常条件下正常工作.在以下条件下遇到问题:
>没有姓名的联系人,这只是号码.
> SIM卡的联系方式.
我的代码不向我提供这两种类型的联系人.我使用以下代码:
public voID getDefaultContactsToDB(){ CallBackDatabase callbackDB = new CallBackDatabase(RequestCallBack.this); callbackDB.open(); //clean the database before entering new values. callbackDB.deletetable(CallBackDatabase.DATABASE_table); ContentResolver cr = getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); if (cur.getCount() > 0) { while (cur.movetoNext()) { String ID = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); Cursor cur1 = cr.query( ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{ID}, null); while (cur1.movetoNext()) { //to get the contact names ArrayList<String> numbers= new ArrayList<String>(); String contactNoumber=""; String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.disPLAY_name)); if(name!=null){ Log.e("name :", name); if(name.equalsIgnoreCase("011999999999999999")){ System.out.println("got it"); } } //to get the contact email String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); if(email!=null) Log.e("Email", email); String hasNoumber = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); if(Integer.parseInt(hasNoumber)>0){ Cursor pCur = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = " +cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)),null, null); int i = 0; while (pCur.movetoNext()) { //to get the contact number contactNoumber = pCur.getString(pCur.getColumnIndex("DATA1")); if(contactNoumber.equalsIgnoreCase("011999999999999999")){ System.out.println("got it"); } contactNoumber = Constant.removeSpecialCharacters(contactNoumber); Log.e("contactNoumber", contactNoumber); // Getting Phone numbers String numType = null; if(pCur.getString(pCur.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)){ switch(pCur.getInt(pCur.getColumnIndex("data2"))){ case ContactsContract.CommonDataKinds.Phone.TYPE_HOME : numType = "HOME"; break; case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE : numType = "MOBILE"; break; case ContactsContract.CommonDataKinds.Phone.TYPE_WORK : numType = "WORK"; break; case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER : numType = "OTHER"; break; case ContactsContract.CommonDataKinds.Phone.TYPE_ASSISTANT : numType ="OTHER"; break; case ContactsContract.CommonDataKinds.Phone.TYPE_CALLBACK : numType = "CALLBACK"; break; case ContactsContract.CommonDataKinds.Phone.TYPE_CAR : numType ="CAR"; break; case ContactsContract.CommonDataKinds.Phone.TYPE_COMPANY_MAIN : numType = "COMPANY MAIN"; break; case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME : numType = "FAX HOME"; break; case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK : numType = "FAX WORK"; break; case ContactsContract.CommonDataKinds.Phone.TYPE_MAIN : numType = "MAIN"; break; case ContactsContract.CommonDataKinds.Phone.TYPE_ISDN : numType = "ISDN"; break; case ContactsContract.CommonDataKinds.Phone.TYPE_MMS : numType = "MMS"; break; case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER_FAX : numType = "OTHER FAX"; break; case ContactsContract.CommonDataKinds.Phone.TYPE_PAGER : numType = "PAGER"; break; case ContactsContract.CommonDataKinds.Phone.TYPE_RAdio : numType = "RAdio"; break; case ContactsContract.CommonDataKinds.Phone.TYPE_TELEX : numType ="TELEX"; break; case ContactsContract.CommonDataKinds.Phone.TYPE_TTY_TDD : numType = "TTY TDD"; break; case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE : numType = "WORK MOBILE"; break; case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_PAGER : numType = "WORK PAGER"; break; case ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM : numType = "CUSTOM"; break; default: break; } } numbers.add(i, contactNoumber+"("+numType+")"); i++; } String numInDB = null; for (int j = 0; j < i; j++) { if(j==0) numInDB =numbers.get(j); else numInDB =numInDB + "," +numbers.get(j); } if(contactNoumber.length()>0){ if(name!=null){ }else{ name = contactNoumber; } callbackDB.InsertContacts(null, name+"="+numInDB, contactNoumber, email); } } } cur1.close(); } //CLOSE DB callbackDB.close(); } }
解决方法:
下面的代码显示了一种简单的方法来读取所有电话号码和名称:
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); while (phones.movetoNext()) { String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.disPLAY_name)); String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); } phones.close();
另外,对于Sim联系人,您可以使用以下代码:
private voID allSIMContact() { try { String m_simPhonename = null; String m_simphoneNo = null; Uri simUri = Uri.parse("content://icc/adn"); Cursor cursorSim = this.getContentResolver().query(simUri,null,null,null,null); Log.i("PhoneContact", "total: "+cursorSim.getCount()); while (cursorSim.movetoNext()) { m_simPhonename =cursorSim.getString(cursorSim.getColumnIndex("name")); m_simphoneNo = cursorSim.getString(cursorSim.getColumnIndex("number")); m_simphoneNo.replaceAll("\D",""); m_simphoneNo.replaceAll("&", ""); m_simPhonename=m_simPhonename.replace("|",""); Log.i("PhoneContact", "name: "+m_simPhonename+" phone: "+m_simphoneNo); } } catch(Exception e) { e.printstacktrace(); }}
编辑:
要获取家庭,移动设备,传真等联系人的详细信息,您需要单独检查以下内容:
总结06002
以上是内存溢出为你收集整理的如何从Android中的电话簿和SIM卡获取所有联系人?全部内容,希望文章能够帮你解决如何从Android中的电话簿和SIM卡获取所有联系人?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)