Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. <uses-permission android:name="android.permission.READ_CONTACTS"/>
  2.  
  3. package com.example.abhishek.detector;
  4.  
  5. import android.app.Activity;
  6. import android.content.ContentResolver;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.content.SharedPreferences;
  10. import android.database.Cursor;
  11. import android.net.Uri;
  12. import android.os.Bundle;
  13. import android.provider.ContactsContract;
  14. import android.support.v7.app.AppCompatActivity;
  15. import android.util.Log;
  16. import android.view.View;
  17. import android.widget.Button;
  18. import android.widget.TextView;
  19.  
  20. import java.util.ArrayList;
  21.  
  22. public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
  23.  
  24. Button Contact;
  25. TextView txt;
  26. private ArrayList<String> contactList;
  27.  
  28. public static final String MyPREFERENCES = "MyPrefs" ;
  29. SharedPreferences sharedpreferences;
  30.  
  31. @Override
  32. public void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.activity_main2);
  35.  
  36. Contact = (Button) findViewById(R.id.button4);
  37. sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
  38. txt = (TextView) findViewById(R.id.textView);
  39.  
  40. Contact.setOnClickListener(this);
  41. }
  42.  
  43.  
  44. @Override
  45. public void onClick(View arg0) {
  46. if (arg0 == Contact) {
  47. try {
  48. Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
  49. startActivityForResult(intent, 1);
  50. SharedPreferences.Editor editor = sharedpreferences.edit();
  51. } catch (Exception e) {
  52. e.printStackTrace();
  53. Log.e("Error in intent : ", e.toString());
  54. }
  55. }
  56. }
  57.  
  58.  
  59. @Override
  60. public void onActivityResult(int reqCode, int resultCode, Intent data) {
  61. super.onActivityResult(reqCode, resultCode, data);
  62.  
  63. try {
  64. if (resultCode == Activity.RESULT_OK) {
  65. Uri contactData = data.getData();
  66. Cursor cur = managedQuery(contactData, null, null, null, null);
  67. ContentResolver contect_resolver = getContentResolver();
  68.  
  69. if (cur.moveToFirst()) {
  70. String id = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
  71. String name = "";
  72. String no = "";
  73.  
  74. Cursor phoneCur = contect_resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
  75. ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
  76.  
  77. if (phoneCur.moveToFirst()) {
  78. name = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
  79. no = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
  80. }
  81.  
  82. Log.e("Phone no & name :***: ", name + " : " + no);
  83. txt.append(name + " : " + no + "n");
  84.  
  85. id = null;
  86. name = null;
  87. no = null;
  88. phoneCur = null;
  89. }
  90. contect_resolver = null;
  91. cur = null;
  92. // populateContacts();
  93. }
  94. } catch (IllegalArgumentException e) {
  95. e.printStackTrace();
  96. Log.e("IllegalArgumentException::", e.toString());
  97. } catch (Exception e) {
  98. e.printStackTrace();
  99. Log.e("Error :: ", e.toString());
  100. }
  101. }
  102.  
  103.  
  104.  
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement