1

My code is as following

Permission

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

And caller Activity code is

Intent addNewContact = new Intent(Intent.ACTION_INSERT);
addNewContact.setType(ContactsContract.Contacts.CONTENT_TYPE);  
startActivityForResult(addNewContact, ADD_NEW_CONTACT); // ADD_NEW_CONTACT = 2 for my specific purpose

And onActivityResult of caller Activity as

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        Log.i("OnActivityResult Test ", "Request code  : " + requestCode
                + "   " + " ResultCode   : " + resultCode);
        switch(requestCode) {           

            case 2: 
                  if (resultCode == Activity.RESULT_OK) {
                // code to Update my list view
                  }

        }
    }

My list view gets update on emulator and device (I checked with samsung galuxy) also other than Droid-X, so result doesn't reflect on list if I am using Droid-X.

When I read Log cat msg of Droid-X I saw resultCode is always 0 (ZERO), even if I am adding new contact.

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186

2 Answers2

0

You could extend the caller Activity and explicitly call setResult when it finishes, maybe?

Sagar Hatekar
  • 8,700
  • 14
  • 56
  • 72
0

I know that the problem with Droid-X, motoblur is that (per moto's website) the blur contacts API is based off of the old Contacts API found in Android 1.x, and not the new 2.x ContactsContract API. It's possible that HTC does the same.

Ref :

  1. Created contacts not showing up on HTC Evo

  2. New contacts created using ContactsContract do not appear in Contacts app

In your case you didn't get result code as -1, when you are adding new contact. So better way don't do any task (if you doing when contact is added) in onActivityResult . Extend ContentObserver class that will receive call backs for changes to content, and you can do your task.

Ref : 1. How to implement an Android ContentObserver

And here is a sample example

public class Test extends Activity {

    private NewContentObserver contentObserver = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout._layout);  

        //do another task

        //Adding listener when new contact will be added in device. 
        contentObserver = new NewContentObserver();
        this.getApplicationContext().getContentResolver().registerContentObserver (ContactsContract.Contacts.CONTENT_URI, true, contentObserver);        
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // unregister the provider 

        this.getApplicationContext().getContentResolver().unregisterContentObserver(contentObserver);
    }

//Get newest contact
    private Uri getNewestContactUri() { 
        String[] projection = new String[] {ContactsContract.Contacts._ID}; 
        String orderBy = ContactsContract.Contacts._ID + " DESC"; 
        Cursor cursor = TagsActivity.this.getContentResolver().query( 
                ContactsContract.Contacts.CONTENT_URI, projection, null, null, orderBy); 
        int idIdx = -1; 
        try { 
                idIdx = cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID); 
        } catch (Exception e) { 
                e.printStackTrace(); 
                idIdx = -1; 
        } 
        if (idIdx != -1) { 
                int id = -1; 
                if (cursor.moveToFirst()) { 
                        id = cursor.getInt(idIdx); 
                } 
                if (id != -1) { 
                        return Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, 
                                        Integer.toString(id)); 
                } 
        } 
        return null; 
    } 

    private class NewContentObserver extends ContentObserver {

        public NewContentObserver() {
            super(null);
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);

            Uri contactData = getNewestContactUri();
        Cursor cursor = managedQuery(contactData, null, null, null, null);
        if (cursor.moveToFirst()) {
        long newId = cursor.getLong(cursor.getColumnIndexOrThrow(Contacts._ID));
        String newDisplayName = cursor.getString(cursor.getColumnIndexOrThrow(Contacts.DISPLAY_NAME));
        Log.i("Test", "New contact Added.  ID of newly added contact is : " + newId + " Name is : " + newDisplayName);
        runOnUiThread(addNewContactToList);
        }
        }

        @Override
        public boolean deliverSelfNotifications() {
            return true;
        }
    }

   //Since we cant update our UI from a thread this Runnable takes care of that! 
    private Runnable addNewContactToList = new Runnable() {
        public void run() {
            //add logic to update your list view
        }
    };
}

Hope this will help.

Update : Contacts 2.x API works on MOTOBLUR phones running Gingerbread (Android 2.3) or higher. My Droid X is running Moto's new Gingerbread, and I'm quite pleased that this now works.

Community
  • 1
  • 1
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186