動態許可權及利用ContentResolver讀取系統的可共享資料
阿新 • • 發佈:2019-01-11
動態許可權對照的參考如下;開啟組中其中一個許可權的授權則組內其他授權也會被開啟;
//需要在6.0後做動態許可權的許可權組,其中開啟某一許可權組中一個則預設開啟組中其他許可權;;;
<!-- CALENDAR-->
<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
<!-- CAMERA-->
<uses-permission android:name="android.permission.CAMERA"/>
<!-- CONTACTS-->
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<!-- LOCATION-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<!-- PHONE-->
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
<uses-permission android:name="android.permission.WRITE_CALL_LOG"/>
<uses-permission android:name="android.permission.USE_SIP"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<!-- SENSORS-->
<uses-permission android:name="android.permission.BODY_SENSORS"/>
<!-- SMS-->
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_MMS"/>
<uses-permission android:name="android.permission.RECEIVE_WAP_PUSH"/>
<!-- STORAGE-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
1: 如果在程式的所需許可權中有包含如下的許可權,則需要使用動態許可權來處理,以免會出現異常的情況;
下面的例子:
//相等則已經授權,不相等則沒有授權,需要請授權; if(ContextCompat.checkSelfPermission(this,Manifest.permission.READ_CONTACTS ) != PackageManager.PERMISSION_GRANTED) {
//請求授權
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_CONTACTS},1);
}else{
//接著做自己的事情
getContacts();
}
2: 授權申請的時候會產生一個回撥函式,如下:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case 1:
if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//接著做自己的事情
getContacts();
}else {
Toast.makeText(ConProActivity.this, "許可權被拒!", Toast.LENGTH_SHORT).show();
}
break;
}
}
下面是利用ContentResolver來獲取系統的共享資料資源,電話號,短息,圖片等等;
private void getContacts() {
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
if(cursor!=null) {
while (cursor.moveToNext()){
int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int numIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String name = cursor.getString(nameIndex);
String num = cursor.getString(numIndex);
Toast.makeText(ConProActivity.this, name+", "+num, Toast.LENGTH_SHORT).show();
}
cursor.close();
}
}