Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ContactTutorialActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
- intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
- startActivityForResult(intent, 1);
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (data != null) {
- Uri uri = data.getData();
- if (uri != null) {
- Cursor c = null;
- try {
- c = getContentResolver().query(uri, new String[]
- {
- ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.TYPE },null, null, null);
- if (c != null && c.moveToFirst()) {
- String number = c.getString(0);
- int type = c.getInt(1);
- showSelectedNumber(type, number);
- }
- } finally {
- if (c != null) {
- c.close();
- }
- }
- }
- }
- }
- public void showSelectedNumber(int type, String number)
- {
- Toast.makeText(this, type + " " + number, Toast.LENGTH_LONG).show();
- }
- }
- ContentResolver cr = getContentResolver();
- Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
- null, null, null, null);
- Log.e("phonenu",""+cur.getCount());
- if (cur.getCount() > 0)
- {
- while (cur.moveToNext())
- {
- String id = cur.getString(
- cur.getColumnIndex(ContactsContract.Contacts._ID));
- //String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
- //Log.e("contacts", name);
- if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
- //Query phone here. Covered next
- Cursor pCur = cr.query(
- ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
- null,
- ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
- new String[]{id}, null);
- while (pCur.moveToNext()) {
- // Do something with phones
- name.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
- number.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
- }
- pCur.close();
- }}}
Add Comment
Please, Sign In to add comment