之前有紀錄一口氣要複數權限的文章
這邊記錄一下另外一種要權限的方式
也就是要到權限才做事
利用onRequestPermissionsResult來達成
1.要權限
<uses-permission android:name="android.permission.CAMERA" />
2.檢查手機版本
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//...進行權限檢查的後續處理 6以上就檢查
}
3.確認這權限是否允許
// Check if the Camera permission is already available.
if ( ActivityCompat.checkSelfPermission( this, Manifest.permission.CAMERA ) == PackageManager.PERMISSION_GRANTED ) {
// already available 已經允許了
} else {
// asking whether to allow permission...不允許所以去問
requestCameraPermission() ;
}
4.後續處理
/**
* Requests the Camera permission.
* If the permission has been denied previously, a SnackBar will prompt the user to grant the
* permission, otherwise it is requested directly.
*/
private void requestCameraPermission() {
Log.i( TAG, "CAMERA permission has NOT been granted. Requesting permission." ) ;
/**
* shouldShowRequestPermissionRationale 回傳的規則如下:
* 1.true:用戶在先前權限詢問的選項中,選擇了「拒絕」,但沒有勾選「不再詢問」
* 2.false:第一次安裝APP後進入該功能
* 3.false:用戶在先前權限詢問選項中,選擇了「允許」
* 4.false:用戶在先前權限詢問的選項中,選擇了「拒絕」,但勾選「不再詢問」
* 5.false:用戶在「設定」裡頭選擇「允許或拒絕」
*
* 呼叫requestPermissions後,是否會跳出詢問視窗,規則如下:
* 1.不會:用戶在先前權限詢問選項中,選擇了「允許」
* 2.不會:用戶在先前權限詢問的選項中,選擇了「拒絕」,但勾選「不再詢問」,在後續onRequestPermissionsResult當中一律回傳PERMISSION_DENIED
* 3.會:第一次安裝APP後進入該功能(第一次跳出不會有「不再詢問」的選項可以勾選)
* 4.會:用戶在先前權限詢問的選項中,選擇了「拒絕」
* 5.會:用戶在「設定」裡頭選擇「允許或拒絕」
*
* 當呼叫requestPermissions後,不管用戶選擇允許或拒絕,又或者沒有跳出詢問視窗,後續都會進入onRequestPermissionsResult執行後續動作,規則如下:
* 1.PERMISSION_GRANTED(允許):用戶選擇「允許」選項、設定中直接選擇「允許」
* 2.PERMISSION_DENIED(拒絕):用戶選擇「拒絕」選項、設定中直接選擇「拒絕」
*/
if ( ActivityCompat.shouldShowRequestPermissionRationale( this, Manifest.permission.CAMERA ) ) {
// Provide an additional rationale to the user if the permission was not granted
// and the user would benefit from additional context for the use of the permission.
// For example if the user has previously denied the permission.
Log.i( TAG, "Displaying camera permission rationale to provide additional context." ) ;
// 「R.string.permission_camera_rationale」的內容可以多加一些說明提醒使用戶該功能必須允許該權限才可以使用
Snackbar.make( mLayout, R.string.permission_camera_rationale, Snackbar.LENGTH_INDEFINITE ).setAction( R.string.ok, new View.OnClickListener() {
@Override
public void onClick( View view ) {
ActivityCompat.requestPermissions( MainActivity.this, new String[] { Manifest.permission.CAMERA }, REQUEST_CAMERA ) ;
}
} ).show() ;
} else {
// Camera permission has not been granted yet. Request it directly.
ActivityCompat.requestPermissions( this, new String[] { Manifest.permission.CAMERA }, REQUEST_CAMERA ) ;
}
}
/**
* Callback received when a permissions request has been completed.
*/
@Override
public void onRequestPermissionsResult( int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults ) {
if ( requestCode == REQUEST_CAMERA ) {
// Received permission result for camera permission.
Log.i( TAG, "Received response for Camera permission request." ) ;
// Check if the only required permission has been granted
if ( grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED ) {
// Camera permission has been granted, preview can be displayed
Log.i( TAG, "CAMERA permission has now been granted. Showing preview." ) ;
Snackbar.make( mLayout, R.string.permision_available_camera, Snackbar.LENGTH_SHORT ).show() ;
} else {
Log.i( TAG, "CAMERA permission was NOT granted." ) ;
Snackbar.make( mLayout, R.string.permissions_not_granted, Snackbar.LENGTH_SHORT ).show() ;
}
}
// other callback
else {
super.onRequestPermissionsResult( requestCode, permissions, grantResults ) ;
}
}
留言列表