如何制作自定义的SMS权限对话框

时间:2019-01-09 16:27:41

标签: android permissions sms

我使用以下代码来获取用户的短信许可。但是我希望在显示权限对话框时添加一些描述。我该怎么办?还是方法呢?谢谢。

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {

        if (checkSelfPermission(Manifest.permission.SEND_SMS)
                == PackageManager.PERMISSION_DENIED) {

            Log.d("permission", "permission denied to SEND_SMS - requesting it");
            String[] permissions = {Manifest.permission.SEND_SMS};

            requestPermissions(permissions, PERMISSION_REQUEST_CODE);

        }
    }

1 个答案:

答案 0 :(得分:1)

基本上,您可以创建一个AlertDialog并在其中添加说明。这是一个实现该目标的示例

try {
    new android.app.AlertDialog.Builder(activity)
            .setMessage("Your description about the permission")
            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String[] permissions = {Manifest.permission.SEND_SMS};
                    requestPermissions(permissions, PERMISSION_REQUEST_CODE);
                }
            })
            .create()
            .show();
} catch (Exception e) {
    // handle exception in case context is not present / destroyed, etc
}

或者,您可以让用户完全不用任何许可就可以发送SMS。在Activity

中创建这些帮助器方法
private boolean sendSms(final String phoneNumber, final String content) {
    Uri uri = Uri.parse("smsto:" + phoneNumber);
    Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
    if (isIntentAvailable(intent)) {
        intent.putExtra("sms_body", content);
        startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
        return true;
    }
    return false;
}

private boolean isIntentAvailable(final Intent intent) {
    return getPackageManager()
            .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
            .size() > 0;
}