java 添加联系人到Android手机...你需要权限

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 添加联系人到Android手机...你需要权限相关的知识,希望对你有一定的参考价值。

public class AddContact{
  
  public static void addContact(){
    String DisplayName = "XYZ";
     String MobileNumber = "123456";
     String HomeNumber = "1111";
     String WorkNumber = "2222";
     String emailID = "email@nomail.com";
     String company = "bad";
     String jobTitle = "abcd";
     
     //<uses-permission android:name="android.permission.READ_CONTACTS" />
     //<uses-permission android:name="android.permission.WRITE_CONTACTS" />

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

}

以上是关于java 添加联系人到Android手机...你需要权限的主要内容,如果未能解决你的问题,请参考以下文章

java - 如何使用json中提供的多个联系人数据在java中的android中添加新的电话联系人

如何在Android系统添加联系人

Android SQLite“动态”添加联系人

如何通过Android手机联系人列表中的代码添加联系人详细信息..?

添加到授予权限后未立即执行的联系人

如何在android中以编程方式将铃声添加到本机应用程序联系人