4

I am making an app for managing contacts. On most phones, everything works fine, but on some Xiaomi devices, I get an error when trying to delete a contact.

enter image description here

Here is my code to delete contact -

final ArrayList ops = new ArrayList();
                            final ContentResolver cr = getContext().getContentResolver();
                            ops.add(ContentProviderOperation
                                    .newDelete(ContactsContract.RawContacts.CONTENT_URI)
                                    .withSelection(ContactsContract.RawContacts.CONTACT_ID + " = ?",
                                            new String[]{contact.getContactId()})
                                    .build());

                            try {
                                cr.applyBatch(ContactsContract.AUTHORITY, ops);
                                LogUtil.log(getString(R.string.log_msg_delete, contact.getDisplayName()));

                            } catch (RemoteException e) {
                                e.printStackTrace();
                            } catch (OperationApplicationException e) {
                                e.printStackTrace();
                            }

I have permissions in the manifest file-

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />

and asking for them in the runtime.

The problem only appears on some Xiaomi devices. On other manufacturers, everything works. Also, I checked other apps from play store and in some contact managers this issue is not present, so there should certainly be a way to fix it

Gk Mohammad Emon
  • 6,084
  • 3
  • 42
  • 42

1 Answers1

1

Go to App permissions > Contacts and see that if AutoPhoner is checked or not. If it is not checked (not allowed) then please check it. And yes, your user needs to do this thing by his hand (Manually allow the permission).

I also faced a problem like you for Xiaomi devices. My app needs user permission from App permissions screen from Sittings (Allow Autostart for my app). I just showed my users the App permissions screen programmatically to give him extra flexibility because manually open App permissions screen is a hassle and bad UX for users. This will help you to do that.

And then if it don't work then I'm quite sure that it is not possible (deleting contact from phone book) in Xiaomi because Xiaomi uses customized OS of Android and they maybe restricted to delete contact from third party apps because of security issue.

Gk Mohammad Emon
  • 6,084
  • 3
  • 42
  • 42