实际上是什么问题?**
如果您有其他代码请发布…
import java.util.ArrayList;import androID.app.Activity;import androID.content.ContentProvIDerOperation;import androID.content.ContentResolver;import androID.content.OperationApplicationException;import androID.database.Cursor;import androID.os.Bundle;import androID.os.remoteexception;import androID.provIDer.ContactsContract;import androID.util.Log;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.Widget.button;import androID.Widget.Toast;public class NativeContentProvIDer extends Activity {/** Called when the activity is first created. */@OverrIDepublic voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.nativecontentprovIDer); button vIEw = (button)findVIEwByID(R.ID.vIEwbutton); button add = (button)findVIEwByID(R.ID.createbutton); button modify = (button)findVIEwByID(R.ID.updatebutton); button delete = (button)findVIEwByID(R.ID.deletebutton); vIEw.setonClickListener(new OnClickListener() { public voID onClick(VIEw v){ displayContacts(); Log.i("NativeContentProvIDer","Completed displaying Contact List"); } }); add.setonClickListener(new OnClickListener() { public voID onClick(VIEw v){ createContact("Mangesh Joshi","123456789"); Log.i("NativeContentProvIDer","Created a new contact,of course hard-coded"); } }); modify.setonClickListener(new OnClickListener() { public voID onClick(VIEw v) { updateContact("Sample name","987654321"); Log.i("NativeContentProvIDer","Completed updating the email ID,if applicable"); } }); delete.setonClickListener(new OnClickListener() { public voID onClick(VIEw v) { deleteContact("Sample name"); Log.i("NativeContentProvIDer","Deleted the just created contact"); } });}private voID displayContacts() { ContentResolver cr = getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null); if (cur.getCount() > 0) { while (cur.movetoNext()) { String ID = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.disPLAY_name)); if (Integer.parseInt(cur.getString( cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{ID},null); while (pCur.movetoNext()) { String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); Toast.makeText(NativeContentProvIDer.this,"name: " + name + ",Phone No: " + phoneNo,Toast.LENGTH_SHORT).show(); } pCur.close(); } } }}private voID createContact(String name,String phone) { ContentResolver cr = getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null); if (cur.getCount() > 0) { while (cur.movetoNext()) { String existname = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.disPLAY_name)); if (existname.contains(name)) { Toast.makeText(NativeContentProvIDer.this,"The contact name: " + name + " already exists",Toast.LENGTH_SHORT).show(); return; } } } ArrayList<ContentProvIDerOperation> ops = new ArrayList<ContentProvIDerOperation>(); ops.add(ContentProvIDerOperation.newInsert(ContactsContract.Contacts.CONTENT_URI) .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE,"accountname@gmail.com") .withValue(ContactsContract.RawContacts.ACCOUNT_name,"com.Google") .build()); ops.add(ContentProvIDerOperation.newInsert(ContactsContract.Contacts.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,0) .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Structuredname.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Structuredname.disPLAY_name,name) .build()); ops.add(ContentProvIDerOperation.newInsert(ContactsContract.Contacts.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,phone) .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,ContactsContract.CommonDataKinds.Phone.TYPE_HOME) .build()); try { cr.applyBatch(ContactsContract.AUTHORITY,ops); } catch (remoteexception e) { // Todo auto-generated catch block e.printstacktrace(); } catch (OperationApplicationException e) { // Todo auto-generated catch block e.printstacktrace(); } Toast.makeText(NativeContentProvIDer.this,"Created a new contact with name: " + name + " and Phone No: " + phone,Toast.LENGTH_SHORT).show();}private voID updateContact(String name,String phone) { ContentResolver cr = getContentResolver(); String where = ContactsContract.Data.disPLAY_name + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ? AND " + String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE) + " = ? "; String[] params = new String[] {name,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_HOME)}; Cursor phoneCur = managedquery(ContactsContract.Data.CONTENT_URI,where,params,null); ArrayList<ContentProvIDerOperation> ops = new ArrayList<ContentProvIDerOperation>(); if ( (null == phoneCur) ) { createContact(name,phone); } else { ops.add(ContentProvIDerOperation.newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection(where,params) .withValue(ContactsContract.CommonDataKinds.Phone.DATA,phone) .build()); } phoneCur.close(); try { cr.applyBatch(ContactsContract.AUTHORITY,"Updated the phone number of 'Sample name' to: " + phone,Toast.LENGTH_SHORT).show();}private voID deleteContact(String name) { ContentResolver cr = getContentResolver(); String where = ContactsContract.Data.disPLAY_name + " = ? "; String[] params = new String[] {name}; ArrayList<ContentProvIDerOperation> ops = new ArrayList<ContentProvIDerOperation>(); ops.add(ContentProvIDerOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI) .withSelection(where,params) .build()); try { cr.applyBatch(ContactsContract.AUTHORITY,"Deleted the contact with name '" + name +"'",Toast.LENGTH_SHORT).show();}}解决方法
import java.util.ArrayList;import androID.app.Activity;import androID.content.ContentProvIDerOperation;import androID.os.Bundle;import androID.provIDer.ContactsContract;import androID.Widget.Toast;public class AddContacts extends Activity{ /** Called when the activity is first created. */ @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); /* try { ContentResolver cr = this.getContentResolver(); ContentValues cv = new ContentValues(); cv.put(ContactsContract.CommonDataKinds.Phone.disPLAY_name,"New name"); cv.put(ContactsContract.CommonDataKinds.Phone.NUMBER,"1234567890"); cv.put(ContactsContract.CommonDataKinds.Phone.TYPE,ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE); cr.insert(ContactsContract.RawContacts.CONTENT_URI,cv); Toast.makeText(this,"Contact added",Toast.LENGTH_LONG).show(); } catch(Exception e) { TextVIEw tv = new TextVIEw(this); tv.setText(e.toString()); setContentVIEw(tv); }*/ String displayname = "XYZ"; String MobileNumber = "123456"; String HomeNumber = "1111"; String WorkNumber = "2222"; String emailID = "email@nomail.com"; String company = "bad"; String jobTitle = "abcd"; ArrayList<ContentProvIDerOperation> ops = new ArrayList<ContentProvIDerOperation>(); ops.add(ContentProvIDerOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE,null) .withValue(ContactsContract.RawContacts.ACCOUNT_name,null) .build()); //------------------------------------------------------ names if(displayname != null) { ops.add(ContentProvIDerOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,0) .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Structuredname.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Structuredname.disPLAY_name,displayname).build()); } //------------------------------------------------------ Mobile Number if(MobileNumber != null) { ops.add(ContentProvIDerOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,MobileNumber) .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) .build()); } //------------------------------------------------------ Home Numbers if(HomeNumber != null) { ops.add(ContentProvIDerOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,HomeNumber) .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,ContactsContract.CommonDataKinds.Phone.TYPE_HOME) .build()); } //------------------------------------------------------ Work Numbers if(WorkNumber != null) { ops.add(ContentProvIDerOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,WorkNumber) .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,ContactsContract.CommonDataKinds.Phone.TYPE_WORK) .build()); } //------------------------------------------------------ Email if(emailID != null) { ops.add(ContentProvIDerOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,0) .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Email.DATA,emailID) .withValue(ContactsContract.CommonDataKinds.Email.TYPE,ContactsContract.CommonDataKinds.Email.TYPE_WORK) .build()); } //------------------------------------------------------ Organization if(!company.equals("") && !jobTitle.equals("")) { ops.add(ContentProvIDerOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY,company) .withValue(ContactsContract.CommonDataKinds.Organization.TYPE,ContactsContract.CommonDataKinds.Organization.TYPE_WORK) .withValue(ContactsContract.CommonDataKinds.Organization.Title,jobTitle) .withValue(ContactsContract.CommonDataKinds.Organization.TYPE,ContactsContract.CommonDataKinds.Organization.TYPE_WORK) .build()); } // Asking the Contact provIDer to create a new contact try { getContentResolver().applyBatch(ContactsContract.AUTHORITY,ops); } catch (Exception e) { e.printstacktrace(); Toast.makeText(getApplicationContext(),"Exception: " + e.getMessage(),Toast.LENGTH_SHORT).show(); } }}总结
以上是内存溢出为你收集整理的如何通过Android手机联系人列表中的代码添加联系人详细信息..?全部内容,希望文章能够帮你解决如何通过Android手机联系人列表中的代码添加联系人详细信息..?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)