Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:将号码添加到具有相同号码类型的现有联系人_Android_Android Contacts - Fatal编程技术网

Android:将号码添加到具有相同号码类型的现有联系人

Android:将号码添加到具有相同号码类型的现有联系人,android,android-contacts,Android,Android Contacts,我正在尝试使用相同的手机,通过编程方式向现有的android联系人添加一个新号码。键入。但“我的代码”将替换给定的手机中的现有手机号码。键入(如果有) 例如,如果一个号码12345678存储在类别Mobile下,我可以通过People的应用程序在同一类别Mobile中添加另一个号码87654321。但是,在运行下面的代码之后,87654321将替换12345678。存储这两个数字的方法是什么。问题是我不知道哪个电话。键入为空以存储新号码。这就是我使用默认类型的原因。下面是我的代码 ArrayLi

我正在尝试使用相同的
手机,通过编程方式向现有的android联系人添加一个新号码。键入
。但“我的代码”将替换给定的
手机中的现有手机号码。键入
(如果有)

例如,如果一个号码
12345678
存储在类别
Mobile
下,我可以通过
People
的应用程序在同一类别
Mobile
中添加另一个号码
87654321
。但是,在运行下面的代码之后,
87654321
将替换
12345678
。存储这两个数字的方法是什么。问题是我不知道哪个
电话。键入
为空以存储新号码。这就是我使用默认类型的原因。下面是我的代码

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

String where1 = ContactsContract.CommonDataKinds.Phone.NUMBER + " = ? ";
String[] params1 = new String[] { oldNumber };
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI).withSelection(where1, params1)
       .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, newNumber)
       .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE).build());

try
{
     cr.applyBatch(ContactsContract.AUTHORITY, ops);
}
catch (RemoteException e)
{
     // TODO Auto-generated catch block
     e.printStackTrace();
}
catch (OperationApplicationException e)
{
     // TODO Auto-generated catch block
     e.printStackTrace();
}
ArrayList ops=new ArrayList();
字符串,其中1=contacts contract.commonDataTypes.Phone.NUMBER+“=?”;
字符串[]参数1=新字符串[]{oldNumber};
添加(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)。带有选择(其中1,参数1)
.withValue(contacts contract.commonDataTypes.Phone.NUMBER、newNumber)
.withValue(contacts contract.commonDataTypes.Phone.TYPE,contacts contract.commonDataTypes.Phone.TYPE_MOBILE).build());
尝试
{
cr.applyBatch(联系合同管理局,ops);
}
捕获(远程异常)
{
//TODO自动生成的捕捉块
e、 printStackTrace();
}
捕获(操作应用程序异常e)
{
//TODO自动生成的捕捉块
e、 printStackTrace();
}

当您进行更新而不是新的插入时,号码会被替换。新编号应为新的插入操作,因为您必须在数据表中创建新条目。为清晰起见,您可以提示用户输入号码类型,或将其与MOBILE type一起插入,然后让用户编辑联系人

// Inserts the specified phone number and type as a Phone data row
op =
        ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)

        .withValue(ContactsContract.Data.RAW_CONTACT_ID, raw_contact_id)

        // Sets the data row's MIME type to Phone
        .withValue(ContactsContract.Data.MIMETYPE,
                ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)

        // Sets the phone number and type
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone)
        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, phoneType);

// Builds the operation and adds it to the array of operations
ops.add(op.build());
公共静态字符串getRawContactId(字符串contactId)
{
字符串res=“”;
Uri Uri=ContactsContract.RawContacts.CONTENT\u Uri;
字符串[]投影=新字符串[]{ContactsContract.RawContacts.\u ID};
字符串选择=contacts contract.raw contacts.CONTACT_ID+“=?”;
字符串[]SelectionAgs=新字符串[]{contactId};
游标c=ContentManager.getContentResolver().query(uri、投影、选择、selectionArgs、null);
如果(c!=null&&c.moveToFirst())
{
res=c.getString(c.getColumnIndex(ContactsContract.RawContacts._ID));
c、 close();
}
返回res;
}
//--------------------------------------------------
公共静态布尔addContactNumber(字符串contactId、字符串newNumber、@Nullable对象类型)
{
尝试
{
if(contactId==null | | newNumber==null)
返回false;
if(type==null)
类型=电话。类型\工作;
ArrayList ops=新的ArrayList();
android.content.ContentProviderOperation.Builder t;
android.content.ContentProviderOperation b;
t=android.content.ContentProviderOperation.newInsert(Data.content\u URI);
t=t.with值(contacts contract.Data.RAW_CONTACT_ID,getraw contactId(contactId));
t=t.withValue(Data.MIMETYPE、Phone.CONTENT\u ITEM\u TYPE);
t=t.withValue(电话号码、新号码);
t=t.withValue(Phone.TYPE,TYPE);
b=t.build();
增列(b)项;
ContentManager.getContentResolver().applyBatch(ContactsContract.AUTHORITY,ops);
返回true;
}
捕获(例外e){}
返回false;
}
//--------------------------------------------------
公共静态布尔updateContactNumber(字符串contactId、字符串oldNumber、字符串newNumber、@Nullable对象类型)
{
尝试
{
如果(contactId==null | | oldNumber==null)
返回false;
if(type==null)
类型=电话。类型\工作;
字符串,其中=contacts contract.Data.CONTACT_ID+“=”和“
+电话号码+“=”和“
+contacts contract.Data.MIMETYPE+“=?”;
String[]numberParams=新字符串[]{contactId,oldNumber,ContactsContract.CommonDataTypes.Phone.CONTENT\u ITEM\u TYPE};
ArrayList ops=新的ArrayList();
android.content.ContentProviderOperation.Builder t;
android.content.ContentProviderOperation b;
t=android.content.ContentProviderOperation.newUpdate(Data.content\u URI);
t=t.1(其中,numberParams);
t=t.withValue(contacts contract.commondatatypes.Phone.NUMBER、newNumber);
t=t.withValue(Phone.TYPE,TYPE);
b=t.build();
增列(b)项;
ContentManager.getContentResolver().applyBatch(ContactsContract.AUTHORITY,ops);
返回true;
}
捕获(例外e){}
返回false;
}
//--------------------------------------------------
样本: addContactNumber(“313”,“+989139277303”,空)


将+989139277303号码添加到联系人

请解释您的答案。只包含代码转储的答案并不是很有帮助。
public static String getRawContactId(String contactId)
{
    String res = "";
    Uri uri = ContactsContract.RawContacts.CONTENT_URI;
    String[] projection = new String[]{ContactsContract.RawContacts._ID};
    String selection = ContactsContract.RawContacts.CONTACT_ID + " = ?";
    String[] selectionArgs = new String[]{ contactId };
    Cursor c = ContentManager.getContentResolver().query(uri, projection, selection, selectionArgs, null);

    if(c != null && c.moveToFirst())
    {
        res = c.getString(c.getColumnIndex(ContactsContract.RawContacts._ID));
        c.close();
    }

    return res;
}
//--------------------------------------------------
public static boolean addContactNumber(String contactId, String newNumber,@Nullable Object type)
{
    try
    {
        if (contactId == null || newNumber == null)
            return false;

        if(type == null)
            type = Phone.TYPE_WORK;

        ArrayList<android.content.ContentProviderOperation> ops = new ArrayList<>();

        android.content.ContentProviderOperation.Builder t ;
        android.content.ContentProviderOperation b ;
        t = android.content.ContentProviderOperation.newInsert(Data.CONTENT_URI);
        t = t.withValue(ContactsContract.Data.RAW_CONTACT_ID, getRawContactId(contactId));
        t = t.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
        t = t.withValue(Phone.NUMBER, newNumber);
        t = t.withValue(Phone.TYPE, type);
        b = t.build();
        ops.add(b);

        ContentManager.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        return true;
    }
    catch (Exception e) {}
    return false;
}
//--------------------------------------------------
public static boolean updateContactNumber(String contactId, String oldNumber, String newNumber,@Nullable Object type)
{
    try
    {
        if (contactId == null || oldNumber == null)
            return false;

        if(type == null)
            type = Phone.TYPE_WORK;

        String where = ContactsContract.Data.CONTACT_ID + " = ? AND "
                + Phone.NUMBER + " = ? AND "
                + ContactsContract.Data.MIMETYPE + " = ?";
        String[] numberParams = new String[]{contactId, oldNumber, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE};

        ArrayList<android.content.ContentProviderOperation> ops = new ArrayList<>();

        android.content.ContentProviderOperation.Builder t ;
        android.content.ContentProviderOperation b ;
        t = android.content.ContentProviderOperation.newUpdate(Data.CONTENT_URI);
        t = t.withSelection(where, numberParams);
        t = t.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, newNumber);
        t = t.withValue(Phone.TYPE, type);
        b = t.build();
        ops.add(b);

        ContentManager.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        return true;
    }
    catch (Exception e) {}
    return false;
}
//--------------------------------------------------