web-dev-qa-db-ja.com

連絡先をAndroid withContactsContractで挿入

新しい連絡先を追加をAndroid 2.2の連絡先に直接追加しようとしています。

//this code doesn't work    
ContentValues cv=new ContentValues();
cv.put(ContactsContract.Contacts.DISPLAY_NAME, "TESTEST");
Uri u= getContentResolver().insert(ContactsContract.Contacts.CONTENT_URI, cv);

「集計連絡先は自動的に作成されます」というエラーが表示されます。私は何が間違っているのですか?

これはAndroidを呼び出します連絡フォームを追加

//this code works but it's not ideal
Intent i = new Intent(Intent.ACTION_INSERT_OR_EDIT);
i.setType(Contacts.CONTENT_ITEM_TYPE);
i.putExtra(Insert.NAME, "TESTTEST");
i.putExtra(Insert.PHONE, "209384");
startActivity(i);

姓と名を送信する方法がわかりません。名ボックスに入力する「名前」のみです。また、郵便番号を通り、市、州、郵便番号に分けて、現在すべて通りのボックスに入れています。

15
Juliet

Androidで連絡先の詳細を追加する

import Java.util.ArrayList;

import Android.app.Activity;
import Android.content.ContentProviderOperation;
import Android.content.ContentProviderResult;
import Android.content.OperationApplicationException;
import Android.net.Uri;
import Android.os.Bundle;
import Android.os.RemoteException;
import Android.provider.ContactsContract;
import Android.provider.ContactsContract.CommonDataKinds.Phone;
import Android.provider.ContactsContract.CommonDataKinds.StructuredName;
import Android.provider.ContactsContract.Contacts.Data;
import Android.provider.ContactsContract.RawContacts;

public class DemoAddAddressBook extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  /*
   * Uri newPerson = addContactName();
   *
   * addMobilePhoneNo(newPerson); addEmail(newPerson);
   * addPostalAddress(newPerson); addOrganization(newPerson);
   */

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

  //Phone Number
  ops.add(ContentProviderOperation
    .newInsert(ContactsContract.Data.CONTENT_URI)
    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,
      rawContactInsertIndex)
    .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
    .withValue(Phone.NUMBER, "9X-XXXXXXXXX")
    .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
    .withValue(Phone.TYPE, "1").build());

  //Display name/Contact name
  ops.add(ContentProviderOperation
    .newInsert(ContactsContract.Data.CONTENT_URI)
    .withValueBackReference(Data.RAW_CONTACT_ID,
      rawContactInsertIndex)
    .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
    .withValue(StructuredName.DISPLAY_NAME, "Mike Sullivan")
    .build());
  //Email details
  ops.add(ContentProviderOperation
    .newInsert(ContactsContract.Data.CONTENT_URI)
    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,
      rawContactInsertIndex)
    .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
    .withValue(ContactsContract.CommonDataKinds.Email.DATA, "[email protected]")
    .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
    .withValue(ContactsContract.CommonDataKinds.Email.TYPE, "2").build());


  //Postal Address

    ops.add(ContentProviderOperation
      .newInsert(ContactsContract.Data.CONTENT_URI)
      .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,
        rawContactInsertIndex)
      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE )
      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POBOX, "Postbox")

      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE )
      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, "street")

      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE )
      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, "city")

      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE )
      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, "region")

      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE )
      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, "postcode")

      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE )
      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, "country")

      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE )
      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, "3")


      .build());


  //Organization details
  ops.add(ContentProviderOperation
    .newInsert(ContactsContract.Data.CONTENT_URI)
    .withValueBackReference(Data.RAW_CONTACT_ID,
      rawContactInsertIndex)
    .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE )
    .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, "Devindia")
    .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE )
    .withValue(ContactsContract.CommonDataKinds.Organization.TITLE, "Developer")
    .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE )
    .withValue(ContactsContract.CommonDataKinds.Organization.TYPE, "0")

    .build());
  //IM details
    ops.add(ContentProviderOperation
      .newInsert(ContactsContract.Data.CONTENT_URI)
      .withValueBackReference(Data.RAW_CONTACT_ID,
        rawContactInsertIndex)
      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE)
      .withValue(ContactsContract.CommonDataKinds.Im.DATA, "ImName")
      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE )
      .withValue(ContactsContract.CommonDataKinds.Im.DATA5, "2")


      .build());
  try {
   ContentProviderResult[] res = getContentResolver().applyBatch(
     ContactsContract.AUTHORITY, ops);
  } catch (RemoteException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (OperationApplicationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }
}
23
Rupesh Kumar
    ContentValues p=new ContentValues();
    p.put(RawContacts.ACCOUNT_TYPE, "com.google");
    p.put(RawContacts.ACCOUNT_NAME, "email");
    Uri rowcontect= getContentResolver().insert(RawContacts.CONTENT_URI, p);
    long rawcontectid=ContentUris.parseId(rowcontect);

    ContentValues value = new ContentValues();
    value.put(Data.RAW_CONTACT_ID,rawcontectid);
    value.put(Android.provider.ContactsContract.Data.MIMETYPE,StructuredName.CONTENT_ITEM_TYPE);
    value.put(StructuredName.DISPLAY_NAME, "kunja gajjar");
    getContentResolver().insert(Android.provider.ContactsContract.Data.CONTENT_URI, value);

    //adding the contents to the data
    ContentValues ppv=new ContentValues();
    ppv.put(Android.provider.ContactsContract.Data.RAW_CONTACT_ID, rawcontectid);
    ppv.put(Android.provider.ContactsContract.Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
    ppv.put(Phone.NUMBER, "975657789");
    ppv.put(Phone.TYPE, Phone.TYPE_MOBILE);
    this.getContentResolver().insert(Android.provider.ContactsContract.Data.CONTENT_URI, ppv);
2
suresh pareek

この回答は役立つようです: https://stackoverflow.com/a/3279117/1259161

ContractsContractAPIを使用して連絡先入力フォームを呼び出す方法について説明します。これらのAPIを使用すると、ContactsContract.CommonDataKinds.StructuredPostalを使用して、必要な追加フィールドを指定することもできます。

2
Andrew
ContentValues cv = new ContentValues();
cv.put(People.NAME, e1.getText().toString());

// e1.getText().tostring() is Contact name 

Uri u =    getContentResolver().insert(People.CONTENT_URI, cv);

Uri pathu = Uri.withAppendedPath(u, People.Phones.CONTENT_DIRECTORY);    

cv.clear();

cv.put(People.NUMBER, e2.getText().toString());

// e2.getText().tostring() is Contact number

getContentResolver().insert(pathu, cv);

Toast.makeText(getApplicationContext(), "Contact Added",Toast.LENGTH_LONG).show();
1
Rjz Satvara