Last active
September 28, 2015 08:38
-
-
Save yanivmo/519ed3e0415fb9837d6f to your computer and use it in GitHub Desktop.
Read Android contacts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The flow starts in onReadClick | |
import android.app.LoaderManager; | |
import android.content.CursorLoader; | |
import android.content.Loader; | |
import android.database.Cursor; | |
import android.provider.ContactsContract; | |
import android.support.v7.app.ActionBarActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
public class ContactsReaderActivity extends ActionBarActivity implements LoaderManager.LoaderCallbacks<Cursor> { | |
private static final int CONTACTS_LOADER = 0; | |
// Columns to read from the Contacts table | |
private static final String[] CONTACTS_PROJECTION = | |
{ | |
ContactsContract.Contacts._ID, | |
ContactsContract.Contacts.LOOKUP_KEY, | |
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY | |
}; | |
// The indixes here must be aligned with the fields order in CONTACTS_PROJECTION | |
private static final int CONTACT_ID = 0; | |
private static final int CONTACT_LOOKUP_KEY = 1; | |
private static final int CONTACT_DISPLAY_NAME = 2; | |
// Columns to read from the Contacts Data table | |
private static final String[] DATA_PROJECTION = | |
{ | |
ContactsContract.Data._ID, | |
ContactsContract.Data.MIMETYPE, | |
ContactsContract.Data.DATA1 | |
}; | |
// The indixes here must be aligned with the fields order in DATA_PROJECTION | |
private static final int CONTACT_MIMETYPE = 1; | |
private static final int CONTACT_DATA1 = 2; | |
private static final String DATA_SELECTION = | |
ContactsContract.Data.LOOKUP_KEY + " = ?" + " AND (" + | |
ContactsContract.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE + "' OR " + | |
ContactsContract.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "')"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_manual_thief); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_manual_thief, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
public void onReadClick(View view) { | |
getLoaderManager().initLoader(CONTACTS_LOADER, null, this); | |
} | |
@Override | |
public Loader<Cursor> onCreateLoader(int id, Bundle args) { | |
return new CursorLoader( | |
this, | |
ContactsContract.Contacts.CONTENT_URI, | |
CONTACTS_PROJECTION, | |
null, null, null | |
); | |
} | |
@Override | |
public void onLoadFinished(Loader<Cursor> loader, Cursor data) { | |
while (data.moveToNext()) { | |
long id = data.getLong(CONTACT_ID); | |
String lookup_key = data.getString(CONTACT_LOOKUP_KEY); | |
String display_name = data.getString(CONTACT_DISPLAY_NAME); | |
String phone = ""; | |
String email = ""; | |
Cursor details = getContentResolver().query( | |
ContactsContract.Data.CONTENT_URI, | |
DATA_PROJECTION, | |
DATA_SELECTION, | |
new String[] {lookup_key}, | |
null); | |
while (details.moveToNext()) { | |
String mime = details.getString(CONTACT_MIMETYPE); | |
if (mime.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) { | |
phone = details.getString(CONTACT_DATA1); | |
} else if (mime.equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)) { | |
email = details.getString(CONTACT_DATA1); | |
} | |
} | |
details.close(); | |
Log.i("ContactsReader", String.format("%d %s : %s %s", id, display_name, phone, email)); | |
} | |
} | |
@Override | |
public void onLoaderReset(Loader<Cursor> loader) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment