安卓 :如何将地址从我们的应用程序存储到本机联系人应用程序中



在Android本机联系人应用程序中,某些手机具有称为Address的单个地址字段。 有些手机有多个字段,例如 street, city, state, country, zip code .只是我想知道如何从我们的应用程序将地址存储到本机应用程序中。以及如何从本机应用程序中检索address

我有同样的问题。很长一段时间后我得到了一个解决方案,所以您可以使用以下代码:

public boolean addContact() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (haveContactPermission()) {
            ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
            int rawContactInsertIndex = ops.size();
            ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                    .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                    .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build());
            ops.add(ContentProviderOperation
                    .newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
                    .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, TextViewUserFullName.getText()) // Name of the person
                    .build());

            ops.add(ContentProviderOperation
                    .newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(
                            ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
                    .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                    .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, homephone) // Number of the person
                    .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_HOME).build()); // Type of mobile number
            ops.add(ContentProviderOperation
                    .newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(
                            ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
                    .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                    .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, mobilephone) // Number of the person
                    .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE).build()); // Type of mobile number
            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
                    .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
                    .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, getBitmapFromURL(image))
                    .build());
            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
                    .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
                    .withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, email)
                    .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_HOME)
                    .build());

            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
                    .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
                    .withValue(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, TextViewWifeName.getText()).build());
            try {
                ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
                return true;
            } catch (RemoteException e) {
                Toast.makeText(ContactInfoZone.this, "" + e.toString(), Toast.LENGTH_SHORT).show();
                // error
                return false;
            } catch (OperationApplicationException e) {
                Toast.makeText(ContactInfoZone.this, "" + e.toString(), Toast.LENGTH_SHORT).show();
                // error
                return false;
            }
        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CONTACTS},
                    PERMISSION_CONTACT_NUMBER);
            return false;
        }
    } else {

        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
        int rawContactInsertIndex = ops.size();
        ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build());
        ops.add(ContentProviderOperation
                .newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, TextViewUserFullName.getText()) // Name of the person
                .build());

        ops.add(ContentProviderOperation
                .newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(
                        ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, homephone) // Number of the person
                .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_HOME).build()); // Type of mobile number
        ops.add(ContentProviderOperation
                .newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(
                        ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, mobilephone) // Number of the person
                .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE).build()); // Type of mobile number
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, getBitmapFromURL(image))
                .build());
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, email)
                .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_HOME)
                .build());

        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, TextViewWifeName.getText()).build());
        try {
            ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
            return true;
        } catch (RemoteException e) {
            Toast.makeText(ContactInfoZone.this, "" + e.toString(), Toast.LENGTH_SHORT).show();
            // error
            return false;
        } catch (OperationApplicationException e) {
            Toast.makeText(ContactInfoZone.this, "" + e.toString(), Toast.LENGTH_SHORT).show();
            // error
            return false;
        }
    }
}

我认为这会对你有所帮助。

它的副本过去也许它会帮助你

     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();
} 

相关内容

  • 没有找到相关文章

最新更新