私は、Marshnmallowからの義務的な手順である許可のチェックを追加するコードを共有しています。たとえば、私はカメラにアクセスする許可を確認します。
private void launchCamera() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MyActivity.this,
Manifest.permission.CAMERA)) {
// Showing explanation to user to require permission
// <Build a dialog here. Following shows how to handle dialog clicks>
dialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Yes, user wants to give permission
dialog.dismiss();
ActivityCompat.requestPermissions(MyActivity.this,
new String[] {Manifest.permission.CAMERA},
Constants.PERMISSION_CAMERA);
}
});
alog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// No, user does not want to give permission now
dialog.dismiss();
}
});
} else {
// No explanation needed, requesting for permission now.
ActivityCompat.requestPermissions(this,
new String[] {Manifest.permission.CAMERA},
Constants.PERMISSION_CAMERA);
}
} else {
// Permission to get location has been granted already.
try {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, Constants.CAMERA_CAPTURE_REQUEST);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
Log.e(TAG, "Camera is not found!");
}
}
}
次に、私たちのダイアログの直後にポップアップするシステムダイアログに提供されたユーザーの応答に基づいて実装する方法があります。
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == Constants.PERMISSION_CAMERA){
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
launchCamera();
} else if (grantResults[0] == PackageManager.PERMISSION_DENIED){
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA)) {
/*Here we have to show a dialog/ something else to show user why app requires permission. This is a good practice.*/
// Handle dialog clicks again
// Build a dialog here
btnPositiveOfDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Yes, I want to give permission now after reading detailed message
dialog.dismiss();
ActivityCompat.requestPermissions(UserInfoActivity.this,
new String[] {Manifest.permission.CAMERA},
Constants.PERMISSION_CAMERA);
}
});
btnNegativeOfDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// No, I still don't understand detailed message and want give permission away
dialog.dismiss();
}
});
} else {
// User checked 'Never ask again'
// Build a dialog here
btnPositiveOfDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Yes, go to app settings and manually enable settings
dialog.dismiss();
final Intent i = new Intent();
i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setData(Uri.parse("package:" + UserInfoActivity.this.getPackageName()));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
MyActivity.this.startActivity(i);
}
});
btnNegativeOfDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// No, I don't want to go to user settings and give permission now.
dialog.dismiss();
}
});
dialog.show();
}
}
}
}
次の2つのオプションがあります。1ターゲットAPIを22以下に下げます。 2 - このチュートリアルの後に新しい許可システムを実装するhttps://developer.android.com/training/permissions/requesting.html – MatPag
@MatPagコードで新しい権限システムを実装し、ターゲットを23に設定しましたが、それでも動作しますそれは低い目標です。 – user1377046