正常情况,权限被拒的情况下程序会抛出异常,但是在MIUI上并不会,试了各种办法,最终用下面的代码搞定了。
public void queryContacts(){
Cursor cursor = null;
try {
cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, null, null, null);
CrossProcessCursorWrapper ac = (CrossProcessCursorWrapper) cursor;
CursorWindow cw = ac.getWindow();
Log.e("ts", "CursorWindow:" + cw);
if (cw == null) {
Toast.makeText(this, "通讯录权限被关闭", Toast.LENGTH_SHORT).show();
}
while (cursor.moveToNext()) {
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i("ts", "number:" + number);
}
} catch (Exception e) {
Log.e("ts", "queryContacts", e);
} finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
}
这里以查询通讯录为例,其他权限也可以这样处理