【问题标题】:Delete contact working on emulator but not on device删除在模拟器上工作但不在设备上工作的联系人
【发布时间】:2012-03-12 17:11:06
【问题描述】:

我编写了一个应用程序来从 android 的联系人列表中删除联系人。它在模拟器中工作正常,但没有删除设备中的联系人。可能是什么问题呢?我正在发布我的代码。请帮助我。

ArrayList ops = new ArrayList();

String[] args = new String[] {contactId};

         // if id is raw contact id
         ops.add(ContentProviderOperation.newDelete(RawContacts.CONTENT_URI).withSelection(RawContacts._ID + "=?", args) .build()); 

         // if id is contact id
         //ops.add(ContentProviderOperation.newDelete(RawContacts.CONTENT_URI).withSelection(RawContacts.CONTACT_ID + "=?", args) .build());
         try {
            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();
        }

使用此代码查找联系人

String get_Number(字符串名称) {

字符串编号 = null;

    String where= "DISPLAY_NAME like ?";
    Cursor  people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, where, new String[]{name}, null);
        people.moveToFirst();  

                try{
                    contactId = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID)); //this is the ID used to delete contact
                    String hasPhone = people.getString(people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                    if ( hasPhone.equalsIgnoreCase("1"))
                        hasPhone = "true";
                    else
                        hasPhone = "false" ;
                    if (Boolean.parseBoolean(hasPhone)) 
                    {
                        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
                        while (phones.moveToNext()) 
                        {
                            number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                           // mConno.add(position,phoneNumber);

                        }
                        phones.close(); 
                    }   

                }       
                catch(Exception e)
                { 

                }



    return number;


}

清单中设置的权限是

 <uses-permission android:name="android.permission.READ_CONTACTS"/>
 <uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.SEND_SMS"/>
 <uses-permission android:name="android.permission.WRITE_SMS" />

刚才我发现我开发的这个应用程序有一个奇怪的行为。在此删除联系人应用程序之前,我开发了一个“添加联系人”应用程序。

所以发生的事情是它无法删除“添加联系人”应用程序添加的联系人,而是删除手动添加的联系人。为什么会发生这种奇怪的事情。

但它在模拟器中运行良好。但在手机上却没有。出了什么问题??

【问题讨论】:

  • 尝试删除联系人时发生了什么?你有任何错误等吗?
  • 它在手机中,我看不到任何错误。它说“联系人已删除”。但实际上并没有删除联系人。
  • 您使用哪些 Android 版本?在设备上?在模拟器中?
  • 请看我编辑的帖子...我的手机有V2.3,模拟器有V2.2
  • 我已经尝试了这个链接stackoverflow.com/questions/7351444/…,仍然没有用,在模拟器上工作但在设备上工作..

标签: android android-emulator android-contacts emulation


【解决方案1】:

您正在尝试使用 联系人 id 删除 原始联系人。 那是行不通的。

一个联系人由多个原始联系人组成,当删除一个联系人时,所有原始联系人也会被删除。

这样做:

long contactId = 12345;
Uri contactUri = Uri.withAppendedPath(Contacts.CONTENT_URI, String.valueOf(contactId));
int deleted = getContentResolver().delete(contactUri, null, null);

如果操作成功,deleted 将为 1。

【讨论】:

    猜你喜欢
    • 2012-08-19
    • 2023-03-23
    • 2013-07-02
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    相关资源
    最近更新 更多