Search This Blog

Thursday, 23 June 2011

Add Email Address from Contact Picker

1). Add permission in to the Menifest.xml
              <uses-permission android:name="android.permission.READ_CONTACTS"/>

2). Pass Intent in on click method

    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
   startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);

3). Add method onActivityResult()



          @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
       if (resultCode == RESULT_OK)
       {
           switch (requestCode)
           {
             case CONTACT_PICKER_RESULT:
               Cursor cursor = null;
               String email = "";
               try
               {
                   Bundle extras = data.getExtras();
                   Set<String> keys = extras.keySet();
                   Iterator<String> iterate = keys.iterator();
                   while (iterate.hasNext())
                   {
                       String key = iterate.next();
                       Log.v(DEBUG_TAG, key + "[" + extras.get(key) + "]");
                   }

                   Uri result = data.getData();
                   Log.v(DEBUG_TAG, "Got a contact result: "
                           + result.toString());

                   // get the contact id from the Uri
                   String id = result.getLastPathSegment();

                   // query for everything email
                   cursor = getContentResolver().query(Email.CONTENT_URI,
                           null, Email.CONTACT_ID + "=?", new String[] { id },
                           null);

                   int emailIdx = cursor.getColumnIndex(Email.DATA);

                   // let's just get the first email
                   if (cursor.moveToFirst()) {

                       /*
                        * Iterate all columns. :) String columns[] =
                        * cursor.getColumnNames(); for (String column :
                        * columns) { int index = cursor.getColumnIndex(column);
                        * Log.v(DEBUG_TAG, "Column: " + column + " == [" +
                        * cursor.getString(index) + "]"); }
                        */

                       email = cursor.getString(emailIdx);

                       Log.v(DEBUG_TAG, "Got email: " + email);

                   } else {
                       Log.w(DEBUG_TAG, "No results");
                   }
               }
               catch (Exception e)
               {
                  Log.e(DEBUG_TAG, "Failed to get email data", e);
               }
               finally
               {
                   if (cursor != null)
                   {
                       cursor.close();
                   }
                 
                   if (email.length() == 0) {
                       Toast.makeText(this, "No email found for contact.",
                               Toast.LENGTH_LONG).show();
                   }
                   else
                   {
                    Global.dbMain.execNonQuery("insert into EmailContacts(email) values('"+ email +"')");
System.out.println("Insert Successfully from contacts");
Intent intent = new Intent(SettingsActivity_Email.this,SettingsActivity_Email.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
                   }

               }

               break;
           }

       } else {
           Log.w(DEBUG_TAG, "Warning: activity result not ok");
       }
   }

No comments:

Post a Comment