Android:一次又一次地请求运行时权限,直到和除非用户接受该权限

vtwuwzda  于 2023-06-04  发布在  Android
关注(0)|答案(1)|浏览(188)

我想请求SEND_SMSREAD_SMS运行时权限(如果未提供)。这是我的代码:

int PERMISSION_ALL = 1;
String[] PERMISSIONS = {
    Manifest.permission.SEND_SMS,
    Manifest.permission.READ_SMS
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    if (!hasPermissions(this, PERMISSIONS)) {
        ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
    }
}
public static boolean hasPermissions(Context context, String... permissions) {
    if (context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
         }
    }
    return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,  int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(requestCode == PERMISSION_ALL){
        if (grantResults.length == 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
            new AlertDialog.Builder(this)
                .setTitle("Permission Needed")
                .setMessage("Please press OK to allow the permissions")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ActivityCompat.requestPermissions(candidate_registration.this, PERMISSIONS, PERMISSION_ALL);
                    }
                 })
                 .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          dialog.dismiss();
                      }
            }).create().show();
        }
    }
}

我连续拒绝两次后,无论我按多少次AlertDialog中的OK按钮,它都不再请求权限,只有AlertDialog一直弹出。如何解决此问题?

cetgtptt

cetgtptt1#

你不能,这是设计好的--为了防止滥用。
你最好还是按照官方规定来。
再次请求权限的唯一方法是将用户导航到应用程序信息,用户必须手动允许它:

fun launchPermissionSettings(context: Context) {
    Intent().apply {
        action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
        data = Uri.fromParts("package", context.packageName, null)

        addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
        addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)

        context.startActivity(this)
    }
}
static void launchPermissionSettings(Context context) {
    Intent intent = new Intent();
    intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
    intent.setData(Uri.fromParts("package", context.getPackageName(), null));

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

    context.startActivity(intent);
}

您还使用了一种已弃用的请求权限的方式。考虑使用ActivityResultContracts.RequestMultiplePermissions()

class MyFragment: Fragment() {
    private lateinit var mRequestPermissionsLauncher: ActivityResultLauncher<Array<String>>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        mRequestPermissionsLauncher = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { result: Map<String, Boolean> ->
            //Handle result here
        }

        //Request permissions like this:
        mRequestPermissionsLauncher.launch(arrayOf(
            Manifest.permission.SEND_SMS,
            Manifest.permission.READ_SMS,
        ))
    }
}
class MyFragment extends Fragment {
    private ActivityResultLauncher<String[]> mRequestPermissionsLauncher;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mRequestPermissionsLauncher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), result -> {
            //Handle activity result here
        });

        //Request permissions like this:
        String[] PERMISSIONS = {
            Manifest.permission.SEND_SMS,
            Manifest.permission.READ_SMS,
        };
        mRequestPermissionsLauncher.launch(PERMISSIONS);
    }
}

相关问题