I recently started my first app for college, and as part of my app I want to acces the contacts of the phone using this guide.
In the guide the onActivityResult looks like this:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request it is that we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// Get the URI that points to the selected contact
Uri contactUri = data.getData();
// We only need the NUMBER column, because there will be only one row in the result
String[] projection = {Phone.NUMBER};
// Perform the query on the contact to get the NUMBER column
// We don't need a selection or sort order (there's only one result for the given URI)
// CAUTION: The query() method should be called from a separate thread to avoid blocking
// your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
// Consider using CursorLoader to perform the query.
Cursor cursor = getContentResolver()
.query(contactUri, projection, null, null, null);
cursor.moveToFirst();
// Retrieve the phone number from the NUMBER column
int column = cursor.getColumnIndex(Phone.NUMBER);
String number = cursor.getString(column);
// Do something with the phone number...
}
}
}
It says i should use a thread or CursorLoader to do the query but so far I was unable to find a good solution for this. If I place the query method in a thread then I can't acces the data from it:
Runnable r = new Runnable() {
@Override
public void run() {
Cursor cursor = getContentResolver()
.query(contactUri, projection, null, null, null);
}
};
Thread queryThread = new Thread(r);
queryThread.start();
cursor.moveToFirst();
int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
So in this code Android Studio cannot resolve the symbol "cursor" :( And so far I could not find a guide I understand on how to do this with CursorLoaders.
You can use an AsyncTask for the job!
And then call the Asynctask like so:
One other way is to do like you did but do all the work inside the thread and then run the result on the UI thread like so:
String number must be final in order to be accessible inside the other (runnable on UI) thread!
Disclaimer: Code is untested.