如果用户第一次拒绝,如何再次请求权限(RunTime)
Posted
技术标签:
【中文标题】如果用户第一次拒绝,如何再次请求权限(RunTime)【英文标题】:How to ask permission (RunTime) again if the user deny for the first time 【发布时间】:2018-01-05 05:09:34 【问题描述】:如果他/她第一次拒绝,我想重新向这种情况下的用户请求许可。我已经设置了权限,但即使我按下 deny 选项,我的应用程序也会运行。我有一个代码应该做我想做的事情,但是当我点击 Alt+Enter 时我得到 Cant resolve symbol Snackbar 它创建了另一个活动并剩下 -make 和-permision_available_camera 出现红色错误。
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults)
if (requestCode == REQUEST_CAMERA)
// BEGIN_INCLUDE(permission_result)
// Received permission result for camera permission.
Log.i(TAG, "Received response for Camera permission request.");
// Check if the only required permission has been granted
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
// Camera permission has been granted, preview can be displayed
Log.i(TAG, "CAMERA permission has now been granted. Showing preview.");
Snackbar.make(mLayout, R.string.permision_available_camera,
Snackbar.LENGTH_SHORT).show();
else
Log.i(TAG, "CAMERA permission was NOT granted.");
Snackbar.make(mLayout, R.string.permissions_not_granted,
Snackbar.LENGTH_SHORT).show();
【问题讨论】:
关注这个链接rathod nilesh它会帮助你 不。我已经这样做了..如果用户拒绝,它将显示所需的权限,但尽管权限被拒绝,应用程序仍会运行。但我想再次弹出 Allow 或 Deny 权限,并且该应用程序仅在用户允许时才运行,否则在拒绝的情况下会出现故障 你可以在 onRequestPermissionsResult() 方法中处理权限结果 如果用户拒绝,我认为你不应该再次请求许可,你所能做的就是,不要让应用程序前进,只是向他们展示一个祝酒词或一个对话框告诉他们这个权限是应用程序正常运行并要求他们在设置中授予权限所必需的。 (当他们在对话框上单击“确定”时可能会将他们带到设置?) 【参考方案1】:试试这个
基本上,我们在这里尝试从外部存储中读取内容。为此,我们必须向用户询问运行时权限。如果用户允许它很好,但如果用户拒绝该权限,它将显示第二个对话框/消息框,直到用户接受该权限
-
请求许可
-
如果用户拒绝,它将在用户启动应用程序时显示“需要权限”对话框
-
如果用户按下确定按钮,它将再次请求权限。
public class MainActivity extends AppCompatActivity
public static int REQUEST_PERMISSION=1;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
permission_fn();
private void requestStoragePermission()
if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE))
new AlertDialog.Builder(this)
.setTitle("Permission Needed")
.setMessage("Permission is needed to access files from your device...")
.setPositiveButton("OK", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
ActivityCompat.requestPermissions(MainActivity.this,new String[]Manifest.permission.READ_EXTERNAL_STORAGE,REQUEST_PERMISSION);
)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
dialog.dismiss();
).create().show();
else
ActivityCompat.requestPermissions(this,new String[]Manifest.permission.READ_EXTERNAL_STORAGE,REQUEST_PERMISSION);
private void permission_fn()
if(ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED)
if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE))
else
ActivityCompat.requestPermissions(this,new String[]Manifest.permission.READ_EXTERNAL_STORAGE,REQUEST_PERMISSION);
else
requestStoragePermission();
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSION)
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
Toast.makeText(this, "Thanks for enabling the permission", Toast.LENGTH_SHORT).show();
//do something permission is allowed here....
else
Toast.makeText(this, "Please allow the Permission", Toast.LENGTH_SHORT).show();
在 androidManifest.xml 文件中添加用户权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
【讨论】:
虽然这段代码 sn-p 可以解决问题,including an explanation 确实有助于提高您的帖子质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。也请尽量不要用解释性的 cmets 挤满你的代码,这会降低代码和解释的可读性! 好的。明白了【参考方案2】:@Arjun Thakun 这里是一种管理再次询问权限概念的方法,并处理当应用询问权限时用户可以回答的所有方式
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
//.... write file into storage ...
System.out.println("SDK > BuildVersion TRUE");
else
requestPermissions(new String[]Manifest.permission.WRITE_EXTERNAL_STORAGE, 666); // Comment 26
System.out.println("go to requestPermissions");
onLoad();
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode)
case 666: // Allowed was selected so Permission granted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
Snackbar s = Snackbar.make(findViewById(android.R.id.content),"Permission Granted",Snackbar.LENGTH_LONG);
View snackbarView = s.getView();
TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.RED);
textView.setTextSize(18);
textView.setMaxLines(6);
s.show();
// do your work here
else if (Build.VERSION.SDK_INT >= 23 && !shouldShowRequestPermissionRationale(permissions[0]))
// User selected the Never Ask Again Option Change settings in app settings manually
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Change Permissions in Settings");
alertDialogBuilder
.setMessage("" +
"\nClick SETTINGS to Manually Set\n"+"Permissions to use Database Storage")
.setCancelable(false)
.setPositiveButton("SETTINGS", new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int id)
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, 1000); // Comment 3.
);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
else
// User selected Deny Dialog to EXIT App ==> OR <== RETRY to have a second chance to Allow Permissions
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED)
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Second Chance");
alertDialogBuilder
.setMessage("Click RETRY to Set Permissions to Allow\n\n"+"Click EXIT to the Close App")
.setCancelable(false)
.setPositiveButton("RETRY", new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int id)
//ActivityCompat.requestPermissions((Activity) context, new String[]Manifest.permission.READ_EXTERNAL_STORAGE, Integer.parseInt(WRITE_EXTERNAL_STORAGE));
Intent i = new Intent(MainActivity.this,MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
)
.setNegativeButton("EXIT", new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int id)
finish();
dialog.cancel();
);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
break;
;
【讨论】:
我的应用程序已经拥有WRITE_SMS
权限OPPO F5 7.1.1
但如果我否认无法在设备旧apk targetsdk was 23
上接收短信,它会自动要求write to sms records/logs permission
使用新的@987654326 更新应用程序@得到问题。不是全新安装以上是关于如果用户第一次拒绝,如何再次请求权限(RunTime)的主要内容,如果未能解决你的问题,请参考以下文章