3.示例代码
@RuntimePermissions
public class MainActivity extends AppCompatActivity {
private AlertDialog alert = null;
private AlertDialog.Builder builder = null;
private Button btnRe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnRe = (Button) this.findViewById(R.id.request_permission);
btnRe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivityPermissionsDispatcher.showCameraWithCheck(MainActivity.this);
}
});
}
@NeedsPermission(Manifest.permission.CAMERA)
void showCamera() {
alert = null;
builder = new AlertDialog.Builder(this);
alert = builder.setIcon(R.mipmap.ic_launcher_round)
.setTitle("成功:")
.setMessage("拿到了相机权限")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你点击了取消按钮~", Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你点击了确定按钮~", Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("中立", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你点击了中立按钮~", Toast.LENGTH_SHORT).show();
}
}).create(); //创建AlertDialog对象
alert.show(); //显示对话框
}
@OnShowRationale(Manifest.permission.CAMERA)
void showRationaleForCamera(final PermissionRequest request) {
alert = null;
builder = new AlertDialog.Builder(this);
alert = builder.setIcon(R.mipmap.ic_launcher_round)
.setTitle("提示:")
.setMessage("您的应用必须要获得相机权限,否则无法正常工作")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
request.cancel();
}
})
.setPositiveButton("同意", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
request.proceed();
}
}).create(); //创建AlertDialog对象
alert.show(); //显示对话框
}
@OnPermissionDenied(Manifest.permission.CAMERA)
void showDeniedForCamera() {
alert = null;
builder = new AlertDialog.Builder(this);
alert = builder.setIcon(R.mipmap.ic_launcher_round)
.setTitle("提示:")
.setMessage("您拒绝了权限,是否重新请求权限")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MainActivityPermissionsDispatcher.showCameraWithCheck(MainActivity.this);
}
}).create(); //创建AlertDialog对象
alert.show(); //显示对话框
}
@OnNeverAskAgain(Manifest.permission.CAMERA)
void showNeverAskForCamera() {
alert = null;
builder = new AlertDialog.Builder(this);
alert = builder.setIcon(R.mipmap.ic_launcher_round)
.setTitle("提示:")
.setMessage("您彻底拒绝了权限,现在是否打开设置去激活?")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("去激活", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.fromParts("package", getPackageName(), null));
startActivity(intent);
}
}).create(); //创建AlertDialog对象
alert.show(); //显示对话框
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
MainActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
}
}