2016-09-13 1 views
0

je laisser l'utilisateur choisir e-mail de contacts avec l'intention suivante:requête Android pour l'email avec id email donné

Intent emailPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Email.CONTENT_URI); 
startActivityForResult(emailPickerIntent, Constants.CONTACT_PICKER_RESULT); 

Comment puis-je interroger pour l'e-mail l'utilisateur a choisi la méthode onActivityResult?

Répondre

1

code suivant vous aidera à obtenir l'adresse e-mail 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 = "", name = ""; 
      try { 
       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._ID + "=" + id, null, null); 

       int nameId = cursor.getColumnIndex(Contacts.DISPLAY_NAME); 
       int emailIdx = cursor.getColumnIndex(Email.DATA); 

       // let's just get the first email 
       if (cursor.moveToFirst()) { 
        email = cursor.getString(emailIdx); 
        name = cursor.getString(nameId); 
        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(); 
       } 
       EditText emailEntry = (EditText) findViewById(R.id.editTextv); 
       EditText personEntry = (EditText) findViewById(R.id.person); 
       emailEntry.setText(email); 
       personEntry.setText(name); 
       if (email.length() == 0 && name.length() == 0) 
       { 
        Toast.makeText(this, "No Email for Selected Contact",Toast.LENGTH_LONG).show(); 
       } 
      } 
      break; 
     } 

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

Vous devez également ajouter la permission suivante dans votre fichier manifeste:

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