java - 权限对话框

标签 java android-studio android-manifest android-permissions

在我的 AndroidManifest.xml 中:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

但是,当我安装了应用程序 APK 后,我必须在 Android 操作系统的设置中手动设置权限(设置 > 应用程序 > myApp > 权限 > 存储)。

如何才能自动设置权限。或者我的应用程序的用户必须在对话框中确认它?

最佳答案

我使用以下代码在我的应用程序中获取权限 字符串

 <string name="permissions_title">Permissions</string>
<string name="draw_over_permissions_message">To display Audio Widget app needs the permission to draw over another apps.</string>
<string name="read_ext_permissions_message">To load list of music app needs access to your media files.</string>
<string name="btn_continue">Continue</string>
<string name="btn_cancel">Cancel</string>
<string name="toast_permissions_not_granted">Permissions not granted.</string>

java

 @TargetApi(Build.VERSION_CODES.M)
private void checkReadStoragePermission() {
    if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.READ_EXTERNAL_STORAGE)) {
            DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (which == DialogInterface.BUTTON_POSITIVE) {

                        ActivityCompat.requestPermissions(ParentActivity.this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, EXT_STORAGE_PERMISSION_REQ_CODE);


                    } else if (which == DialogInterface.BUTTON_NEGATIVE) {
                        onPermissionsNotGranted();

                    }
                    dialog.dismiss();
                    finish();
                    startActivity(getIntent());
                }
            };
            new AlertDialog.Builder(this)
                    .setTitle(R.string.permissions_title)
                    .setMessage(R.string.read_ext_permissions_message)
                    .setPositiveButton(R.string.btn_continue, onClickListener)
                    .setNegativeButton(R.string.btn_cancel, onClickListener)
                    .setCancelable(false)
                    .show();
            return;
        }
        ActivityCompat.requestPermissions(ParentActivity.this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.READ_PHONE_STATE}, EXT_STORAGE_PERMISSION_REQ_CODE);
        return;
    }

}

private void onPermissionsNotGranted() {
    Toast.makeText(this, R.string.toast_permissions_not_granted, Toast.LENGTH_SHORT).show();
    Log.v("tom", "JERRY");
}


@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void checkwriteStoragePermission() {
    if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (which == DialogInterface.BUTTON_POSITIVE) {

                        ActivityCompat.requestPermissions(ParentActivity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_EXTERNAL_STORAGE);
                        Log.v("tom", "TOM");
                    } else if (which == DialogInterface.BUTTON_NEGATIVE) {
                        onPermissionsNotGranted();
                        Log.v("tom", "JERRY");
                    }
                    dialog.dismiss();
                }
            };
            new AlertDialog.Builder(this)
                    .setTitle(R.string.permissions_title)
                    .setMessage(R.string.read_ext_permissions_message)
                    .setPositiveButton(R.string.btn_continue, onClickListener)
                    .setNegativeButton(R.string.btn_cancel, onClickListener)
                    .setCancelable(false)
                    .show();
            return;
        }
        ActivityCompat.requestPermissions(ParentActivity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_EXTERNAL_STORAGE);
        return;
    }

}

 private static final int WRITE_EXTERNAL_STORAGE = 4;
private static final int READ_PHONE_STATE = 3;

您可以在任何需要的地方调用该方法。

关于java - 权限对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44435490/

相关文章:

java - 使用 SOAPHandler 时 MTOM 不工作

java - google guava lib 中 newArrayList(E... elements) 的好奇心

java - 如何对 java.util.Arrays.toString 使用静态导入?

android - 使用 Android Studio 为一个模块重建、同步 Gradle 和清理

java - 如何在 Android Studio 中导入并使用 txt 文件?

java - 如何使 jButton 操作不仅仅在一次运行中起作用?

android - 如何让光标跳转到Android Studio实时模板中的特定位置?

android - 从快捷方式启动 Activity 总是同时启动主 Activity

android - 为什么我按下按钮时会收到此错误消息?

git - repo sync 命令的替代方案是什么?