我正在开发一个 android 短信应用程序,我想在其中使用以下代码从联系人中读取联系人号码
Intent ContactPickerIntent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
ContactPickerIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(ContactPickerIntent, CONTACT_PICKER_RESULT);
它在模拟器上运行良好,但在设备上运行良好。我应该进行哪些更改才能使其在设备上运行。
这是我的 onactivityresult 函数
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK)
{
switch (requestCode)
{
case CONTACT_PICKER_RESULT:
Cursor cursor=null;
try
{
Uri result = data.getData();
Log.v(DEBUG_TAG, "Got a contact result: " + result.toString());
// get the contact id from the Uri
String id = result.getLastPathSegment();
// query for everything contact number
cursor = getContentResolver().query(
Phone.CONTENT_URI, null,
Phone.CONTACT_ID + "=?",
new String[]{id}, null);
cursor.moveToFirst();
int phoneIdx = cursor.getColumnIndex(Phone.NUMBER);
if (cursor.moveToFirst())
{
phonenofromcontact = cursor.getString(phoneIdx);
finallistofnumberstosendmsg +=","+phonenofromcontact;
Log.v(DEBUG_TAG, "Got phone no : " + phonenofromcontact);
}
else
{
Log.w(DEBUG_TAG, "No results");
}
}
catch(Exception e)
{
Log.e(DEBUG_TAG, "Failed to get contact number", e);
}
finally
{
if (cursor != null)
{
cursor.close();
}
}
phonePhoneno= (EditText)findViewById(R.id.phonenofromcontact);
phonePhoneno.setText(finallistofnumberstosendmsg);
//phonePhoneno.setText(phonenofromcontact);
if(phonenofromcontact.length()==0)
{
Toast.makeText(this, "No contact number found for this contact",
Toast.LENGTH_LONG).show();
}
break;
}
}
else
{
Log.w(DEBUG_TAG, "Warning: activity result not ok");
}
}