Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. btngetContact.setOnClickListener(new View.OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4.  
  5. Intent intent1 = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
  6.  
  7. ((Activity)context). startActivityForResult(intent1, RESULT_PICK_CONTACT);
  8. }
  9. });
  10.  
  11. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  12. Toast.makeText(context, "hiiii", Toast.LENGTH_SHORT).show();
  13. if(resultCode==RESULT_OK)
  14.  
  15. {
  16.  
  17. switch (requestCode) {
  18.  
  19. case RESULT_PICK_CONTACT:
  20.  
  21. getSelectedContact (data);
  22.  
  23. break;
  24.  
  25. }
  26.  
  27. }
  28.  
  29. else
  30.  
  31. {
  32.  
  33. Toast.makeText(context, "Faild", Toast.LENGTH_SHORT).show();
  34.  
  35. }
  36. }
  37. private void getSelectedContact(Intent data) {
  38.  
  39. Cursor cursor = null;
  40.  
  41. try {
  42.  
  43. String phoneNo = null;
  44.  
  45. Uri uri = data.getData ();
  46.  
  47. cursor = ((Activity)context).getContentResolver ().query (uri, null, null,null,null);
  48.  
  49. cursor.moveToFirst ();
  50.  
  51. int phoneIndex = cursor.getColumnIndex (ContactsContract.CommonDataKinds.Phone.NUMBER);
  52.  
  53. phoneNo = cursor.getString (phoneIndex);
  54.  
  55. phone.setText (phoneNo);
  56.  
  57. } catch (Exception e) {
  58.  
  59. e.printStackTrace ();
  60.  
  61. }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement