遇到一个bug,酷派手机在执行以下代码时会报错
Intent i = new Intent(Intent.ACTION_PICK); i.setType("vnd.android.cursor.dir/phone"); startActivityForResult(i, SELECTNUMBER);
protected void onActivityResult(int requestCode, int resultCode, Intent dataIntent) { super.onActivityResult(requestCode, resultCode, dataIntent); if (requestCode == SELECTNUMBER && resultCode == RESULT_OK) { if (dataIntent == null) { return; } Uri uri = dataIntent.getData(); Cursor cursor = getContentResolver().query(uri, null, null, null, null); if (cursor.moveToFirst()) {
name.setText(cursor.getString(cursor.getColumnIndexOrThrow(Contacts.Phones.NAME)) + ""); phone.setText(cursor.getString(cursor.getColumnIndexOrThrow(Contacts.Phones.NUMBER)) + "");
if (Build.VERSION.SDK_INT < 14) { cursor.close(); }
}
}
应该是因为没有实现返回带电话的联系人信息导致的。注释掉 i.setType("vnd.android.cursor.dir/phone"); 即可正常运行,但是在
onActivityResult无法通过游标获取到联系人姓名和电话。 网上查了后改为先读取联系人id,然后再去查找电话号码,这么一改代码臃肿了很多,不过为了兼容酷派也没办法.代码如下:
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); if (getPackageManager().resolveActivity(intent, 0) != null) { startActivityForResult(intent, SELECTNUMBER); } else { showMsg("您的手机不支持选择通讯录联系人"); }
protected void onActivityResult(int requestCode, int resultCode, Intent dataIntent) { super.onActivityResult(requestCode, resultCode, dataIntent); if (requestCode == SELECTNUMBER && resultCode == RESULT_OK) { if (dataIntent == null) { return; } Uri contactData = dataIntent.getData(); Cursor cursor = managedQuery(contactData, null, null, null, null); String id = ""; if (cursor.moveToFirst()) { name.setText(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)) + "");//name为TextView String haanumber = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); if (haanumber.equalsIgnoreCase("1")) { Cursor phones =getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, null, null); while (phones.moveToNext()) { phone.setText(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));//phone为TextView
} if (Build.VERSION.SDK_INT < 14) { phones.close(); } } if (Build.VERSION.SDK_INT < 14) { cursor.close(); } } }}
时间: 2024-11-01 14:27:03