如何在android中添加新的联系人
我已经写了下面的代码,以添加新的联系人在Android电话簿,它正在工作,但是当我打开联系人菜单,我看不到新的联系人添加。 任何人都可以帮我找出这里有什么问题吗?
import android.app.Activity; import android.os.Bundle; import android.content.ContentResolver; import android.content.ContentValues; import android.provider.ContactsContract; import android.widget.TextView; 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, 0) .withValue(ContactsContract.Data.MIMETYPE, 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, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, 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, 0) .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, 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, 0) .withValue(ContactsContract.Data.MIMETYPE, 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(myContext, "Exception: " + e.getMessage(), Toast.LENGTH_SHORT).show(); }
这是代码。 根据您的需要进行整合。 我希望这会有所帮助。
这对我来说工作得很好:
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); int rawContactInsertIndex = ops.size(); ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI) .withValue(RawContacts.ACCOUNT_TYPE, null) .withValue(RawContacts.ACCOUNT_NAME, null).build()); ops.add(ContentProviderOperation .newInsert(Data.CONTENT_URI) .withValueBackReference(Data.RAW_CONTACT_ID,rawContactInsertIndex) .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE) .withValue(StructuredName.DISPLAY_NAME, "Vikas Patidar") // Name of the person .build()); ops.add(ContentProviderOperation .newInsert(Data.CONTENT_URI) .withValueBackReference( ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex) .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE) .withValue(Phone.NUMBER, "9999999999") // Number of the person .withValue(Phone.TYPE, Phone.TYPE_MOBILE).build()); // Type of mobile number try { ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); } catch (RemoteException e) { // error } catch (OperationApplicationException e) { // error }
这些例子都很好,我想指出,你可以使用Intent来达到同样的结果。 意图打开“联系人”应用程序,其中包含您已提供的字段。
用户可以保存新创build的联系人。
你可以在这里阅读: https : //developer.android.com/training/contacts-provider/modify-data.html
Intent contactIntent = new Intent(ContactsContract.Intents.Insert.ACTION); contactIntent.setType(ContactsContract.RawContacts.CONTENT_TYPE); contactIntent .putExtra(ContactsContract.Intents.Insert.NAME, "Contact Name") .putExtra(ContactsContract.Intents.Insert.PHONE, "5555555555"); startActivityForResult(contactIntent, 1);
startActivityForResult()使您有机会看到结果。
我注意到resultCode在> 5.0的设备上工作,
但我有一个旧的三星(<5)总是返回RESULT_CANCELLED(0)。
我知道这是默认返回,如果一个活动不希望返回任何东西。
@Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); if (requestCode == 1) { if (resultCode == Activity.RESULT_OK) { Toast.makeText(this, "Added Contact", Toast.LENGTH_SHORT).show(); } if (resultCode == Activity.RESULT_CANCELED) { Toast.makeText(this, "Cancelled Added Contact", Toast.LENGTH_SHORT).show(); } } }
这不是说上面的答案是不正确的,但我觉得这个代码非常容易理解,因此我在这里与大家分享。 还有对WRITE_CONTACTS
权限的检查。
以下是如何将电话号码,电子邮件,网站等添加到现有联系人的完整代码。
public static void addNumberToContact(Context context, Long contactRawId, String number) throws RemoteException, OperationApplicationException { addInfoToAddressBookContact( context, contactRawId, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_OTHER, number ); } public static void addEmailToContact(Context context, Long contactRawId, String email) throws RemoteException, OperationApplicationException { addInfoToAddressBookContact( context, contactRawId, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE, ContactsContract.CommonDataKinds.Email.ADDRESS, ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_OTHER, email ); } public static void addURLToContact(Context context, Long contactRawId, String url) throws RemoteException, OperationApplicationException { addInfoToAddressBookContact( context, contactRawId, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE, ContactsContract.CommonDataKinds.Website.URL, ContactsContract.CommonDataKinds.Website.TYPE, ContactsContract.CommonDataKinds.Website.TYPE_OTHER, url ); } private static void addInfoToAddressBookContact(Context context, Long contactRawId, String mimeType, String whatToAdd, String typeKey, int type, String data) throws RemoteException, OperationApplicationException { if(ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_CONTACTS) == PackageManager.PERMISSION_DENIED) { return; } ArrayList<ContentProviderOperation> ops = new ArrayList<>(); ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValue(ContactsContract.Data.RAW_CONTACT_ID, contactRawId) .withValue(ContactsContract.Data.MIMETYPE, mimeType) .withValue(whatToAdd, data) .withValue(typeKey, type) .build()); getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); }