startActivityForResult(...) and onActivityResult(...) not returning any value
in
Android Processing
•
2 years ago
Hi,
So I was trying to build an app which allows users to access the phone book on users phone and add some info from there into the application. I have the following snippet of code which creates a "sub-activity" using the startActivityForResult(intent, int) and it successfully launches the phonebook. So I click on the particular contact and then this "sub-activity" returns control to my processing app without returning any information about the contact. It even fails to print any statements in the console so I have no idea what is happening here. Can someone help me with the problem here? Appreciate the help.
CK
So I was trying to build an app which allows users to access the phone book on users phone and add some info from there into the application. I have the following snippet of code which creates a "sub-activity" using the startActivityForResult(intent, int) and it successfully launches the phonebook. So I click on the particular contact and then this "sub-activity" returns control to my processing app without returning any information about the contact. It even fails to print any statements in the console so I have no idea what is happening here. Can someone help me with the problem here? Appreciate the help.
- import android.content.Intent;
- import android.provider.ContactsContract;
- import android.database.Cursor;
- final static int ACTIVITY_CREATE = 1;
- .....
- Intent intentContact = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
- startActivityForResult(intentContact, ACTIVITY_CREATE);
- ......
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent intent)
- {
- super.onActivityResult(requestCode, resultCode, intent);
- if(resultCode == RESULT_OK){
- switch(requestCode) {
- case ACTIVITY_CREATE:
- try{
- println("Hello");
- getContactInfo(intent);
- }catch(NullPointerException e){
- println(e);
- }break;
- }
- }
- }
- public void getContactInfo(Intent intent)
- {
- println("here");
- Cursor cursor = managedQuery(intent.getData(), null, null, null, null);
- while (cursor.moveToNext())
- {
- String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
- String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
- println("Name of contact: "+name);
- String hasPhone = cursor.getString(cursor.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())
- {
- String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
- println("Contacts phone number: "+phoneNumber);
- }
- phones.close();
- }
- // Find Email Addresses
- Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,null, null);
- while (emails.moveToNext())
- {
- String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
- println("Contacts email address: "+emailAddress);
- }
- emails.close();
- }
- cursor.close();
- }//getContactInfo
CK
1