Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. package com.kamal.sos10;
  2.  
  3. import android.content.ContentResolver;
  4. import android.database.Cursor;
  5. import android.net.Uri;
  6. import android.provider.ContactsContract;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.content.Intent;
  13. import android.widget.Toast;
  14. public class Contact extends AppCompatActivity {
  15.  
  16. EditText msg,editText2,editText3,editText4;
  17. Button con1,con2,con3;
  18. static final int PICK_CONTACT = 1;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_contact);
  23.  
  24. msg=(EditText)findViewById(R.id.msg);
  25. editText2=(EditText)findViewById(R.id.editText2);
  26. editText3=(EditText)findViewById(R.id.editText3);
  27. editText4=(EditText)findViewById(R.id.editText4);
  28.  
  29. con1=(Button)findViewById(R.id.con1);
  30. con2=(Button)findViewById(R.id.con2);
  31. con3=(Button)findViewById(R.id.con3);
  32.  
  33. con1.setOnClickListener(new View.OnClickListener() {
  34. @Override
  35. public void onClick(View view) {
  36. Intent intent = new Intent(Intent.ACTION_PICK);
  37. intent.putExtra("extra_text1", "1");
  38. intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
  39. if (intent.resolveActivity(Contact.this.getPackageManager()) != null) {
  40. startActivityForResult(intent, PICK_CONTACT);
  41. }
  42. }
  43. });
  44.  
  45. con2.setOnClickListener(new View.OnClickListener() {
  46. @Override
  47. public void onClick(View view) {
  48. Intent intent = new Intent(Intent.ACTION_PICK);
  49. intent.putExtra("extra_text2", "2");
  50. intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
  51. if (intent.resolveActivity(Contact.this.getPackageManager()) != null) {
  52. startActivityForResult(intent, PICK_CONTACT);
  53. }
  54. }
  55. });
  56.  
  57. con3.setOnClickListener(new View.OnClickListener() {
  58. @Override
  59. public void onClick(View view) {
  60. Intent intent = new Intent(Intent.ACTION_PICK);
  61. intent.putExtra("extra_text3", "3");
  62. intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
  63. if (intent.resolveActivity(Contact.this.getPackageManager()) != null) {
  64. startActivityForResult(intent, PICK_CONTACT);
  65. }
  66. }
  67. });
  68.  
  69.  
  70. }
  71. @Override
  72. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  73. super.onActivityResult(requestCode, resultCode, data);
  74. String text1 = getIntent().getStringExtra("extra_text1");
  75. Toast.makeText(Contact.this,text1,Toast.LENGTH_SHORT).show();
  76. if (requestCode == PICK_CONTACT) {
  77. if (resultCode == this.RESULT_OK) {
  78. contactPicked(data);
  79. }
  80. }
  81. }
  82.  
  83. private void contactPicked(Intent data) {
  84. ContentResolver cr = this.getContentResolver();
  85. Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
  86. cur.moveToFirst();
  87. try {
  88. // getData() method will have the Content Uri of the selected contact
  89. Uri uri = data.getData();
  90. //Query the content uri
  91. cur = this.getContentResolver().query(uri, null, null, null, null);
  92. cur.moveToFirst();
  93. // column index of the contact ID
  94. String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
  95. // column index of the contact name
  96. String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
  97. // column index of the phone number
  98. Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
  99. ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
  100. new String[]{id}, null);
  101. while (pCur.moveToNext()) {
  102. String phone = pCur.getString(
  103. pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replaceAll(" ", "");
  104. /*String text1 = getIntent().getStringExtra("extra_text1");
  105. Toast.makeText(Contact.this,text1,Toast.LENGTH_SHORT).show();
  106. if (text1.equals("1")) {
  107. editText2.setText(phone);
  108. }
  109. String text2 = getIntent().getStringExtra("extra_text2");
  110. if (text2 == "2") {
  111. editText3.setText(phone);
  112. }
  113. String text3 = getIntent().getStringExtra("extra_text3");
  114. if (text3 == "3") {
  115. editText4.setText(phone);
  116. */}
  117.  
  118.  
  119. pCur.close();
  120. }
  121. catch (Exception e) {
  122. e.printStackTrace();
  123. }
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement