如何在不打开“联系人”应用程序的情况下将新联系人插入Android联系人?

时间:2014-07-10 03:55:51

标签: java android android-contacts

我有以下代码,我在联系人列表中添加了新的联系人。

    Intent intent = new Intent(Intent.ACTION_INSERT);
    intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
    intent.putExtra(ContactsContract.Intents.Insert.NAME, "John");
    intent.putExtra(ContactsContract.Intents.Insert.PHONE, "1234");
    int PICK_CONTACT = 100;
    this.startActivityForResult(intent, PICK_CONTACT);

最后一行打开了默认的Android应用,可以让我们添加新的联系人,正确的部分用" John"和" 1234"。我想要的是我的应用程序完成添加该联系人,甚至没有打开默认应用程序。理想情况下,这意味着以编程方式点击通讯录应用上的完成按钮。这可能吗?如果是这样,怎么样?

2 个答案:

答案 0 :(得分:1)

以下是添加联系人的示例。它会在不打开默认联系人的情况下为设备联系人添加联系人。

FirstName = FNBox.getText().toString();
LastName = LNBox.getText().toString();
PhoneNo   = number;
ContactAdd Contact = new     ContactAdd(FirstName,LastName,PhoneNo,getApplicationContext(),imageByteArray);
                           ArrayList<ContentProviderOperation> contacts =     Contact.AddContact();
                           try {
                               ContentProviderResult[] res =     getContentResolver().applyBatch(ContactsContract.AUTHORITY, contacts);
                               Log.d("myContacts", "contacts added");
                           } catch (RemoteException e) {
                               // TODO Auto-generated catch block
                               e.printStackTrace();
                           } catch (OperationApplicationException e) {
                               // TODO Auto-generated catch block
                               e.printStackTrace();
                           }

ContactAdd指的是这个类......

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentProviderOperation;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.util.Log;


public class ContactAdd extends Activity {

  private String FirstName,LastName,PhoneNumber;
  private  Context mContext;
  private  byte[] photoByteArray = null;

  public ContactAdd(String First,String Last,String Phone,Context context)
  {
      this.FirstName   = First;
      this.LastName    = Last;
      this.PhoneNumber = Phone;
      this.mContext = context;
  }

  public ContactAdd(String First,String Last,String Phone,Context context,byte[] Photo)
  {
        this.FirstName   = First;
        this.LastName    = Last;
        this.PhoneNumber = Phone;
        this.mContext = context;
        this.photoByteArray = Photo;
  }

  public ArrayList<ContentProviderOperation> AddContact()
  {
     final ArrayList<ContentProviderOperation> contacts = new ArrayList<ContentProviderOperation>();
     final int rawContactInsertIndex = contacts.size();
     SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(mContext);
     Log.d("AddMeNow", preference.getString("accountKey", null) + "  "+preference.getString("emailKey", null));
            // TODO Auto-generated method stub
              contacts.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
                        .withValue(RawContacts.ACCOUNT_TYPE, preference.getString("accountKey",null))
                        .withValue(RawContacts.ACCOUNT_NAME, preference.getString("emailKey",null)).build());

             //Add Name
             contacts.add(ContentProviderOperation
                     .newInsert(Data.CONTENT_URI)
                     .withValueBackReference(Data.RAW_CONTACT_ID,0 )
                     .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
                     .withValue(StructuredName.DISPLAY_NAME, FirstName+" "+LastName)
                     .build());


             //Add Phone Number
             contacts.add(ContentProviderOperation
                     .newInsert(Data.CONTENT_URI)
                     .withValueBackReference(Data.RAW_CONTACT_ID,
                     0 )
                     .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
                     .withValue(Phone.NUMBER, PhoneNumber)
                     .withValue(Phone.TYPE, Phone.TYPE_MOBILE)
                     .build());

                    contacts.add(ContentProviderOperation
                                .newInsert(ContactsContract.Data.CONTENT_URI)
                                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                                .withValue(ContactsContract.Data.MIMETYPE,        ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
                                .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO,     photoByteArray).build());


       return contacts;


      }

    }

答案 1 :(得分:0)

您应该使用联系人ContentProvider。