0

I'm messing with the following code to make it run on all the devices. Fortunately, it works perfectly fine on LG Nexus 5 (Android 6.0.1)

The same piece of code I'm trying on Sony Xperia D6503 (Android 6.0.1). But it's not working.

private void addContactUsingIntent() {
        Intent intent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
        intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
        intent.putExtra(INTENT_KEY_FINISH_ACTIVITY_ON_SAVE_COMPLETED, true);
        //ContactsListActivity.this.startActivityForResult(intent, INSERT_CONTACT_REQUEST);
        startActivityForResult(intent, INSERT_CONTACT_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == INSERT_CONTACT_REQUEST) {
            if (resultCode == RESULT_OK) {
                Uri contactData = data.getData();

                Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
                if (cursor.moveToFirst()) {
                    long newId = cursor.getLong(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

                    String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));

                    String email = getEmail(String.valueOf(newId));

                    ArrayList<String> phones = new ArrayList<>();

                    Cursor cPhones = getContentResolver().query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                            new String[]{String.valueOf(newId)}, null);

                    while (cPhones.moveToNext()) {
                        phones.add(cPhones.getString(cPhones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                    }

                    cursor.close();
                    cPhones.close();

                    Uri photoUri = getPhotoUri(newId);
                    Log.i("Photo URI", photoUri.toString());

                    updateViews(newId, name, phones, email, photoUri);
                }
            }
        }
    }

Please have a look at the above code and let me know if I'm doing anything wrong.

On LG Nexus 5 device, when my app calls addContactUsingIntent the default Contact application of the device opens. As soon as users saves the contact, I'm getting data in onActivityResult. On Sony Xperia device, the default Contact screen to add contact also opens, but after users saves it, it displays the newly added contact and then user has to manually press back to return to my application. Doing so, when onActivityResult" is called, it comes back with data as null andresultCodeas-1`, which is obviously not the case.

I've also tried the following solution, but it's not working on this device. Add contact intent doesn't return data onActivityResult under ICS

Any suggestions to make it work will be greatly appreciated.

Thanks

P.S. I've also tested this code on Oppo F3 Plus (Android 6.0.1) and it's working perfectly fine.

Travolta John
  • 57
  • 1
  • 6

1 Answers1

0

The problem lies in the app that is coming up for your Intent on your Sony XPERIA device. They obviously did not test ACTION_INSERT very well (though your INTENT_KEY_FINISH_ACTIVITY_ON_SAVE_COMPLETED is not part of the Android SDK and is likely to be ignored).

However, there is nothing that you can do about this, unless you are a Sony employee and can somehow fix the bug in their contacts app.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Then how can I make sure that this code runs on all Android devices? Any clues? Thank you for your prompt and quick response. – Travolta John May 31 '18 at 12:05
  • @TravoltaJohn: "how can I make sure that this code runs on all Android devices?" -- you can't. Whenever you integrate with somebody else's app, there is always the chance that the other app has bugs. Either live with the fact that other apps are unreliable, or do not integrate with other apps. – CommonsWare May 31 '18 at 12:29