在我的应用程序中,我已经创建了一个类名Ungic,在那里我拧READ_EXTERNAL_STORAGE权限的代码。但问题是,当我点击拒绝应用程序,我没有找到警报通知设置它允许再次。起初,我有“永远不要再问”的选择。现在运行应用程序后,如果我点击拒绝,我不能再找到对话框消息,使其再次允许。我如何修改我的代码来每次显示此消息。
我的实用课程是
public class Utility {
public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 123;
@TargetApi(Build.VERSION_CODES.M)
public static boolean checkPermission(final Context context) {
int currentAPIVersion = Build.VERSION.SDK_INT;
if (currentAPIVersion >= android.os.Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_EXTERNAL_STORAGE)) {
Log.v("TAG", "Permission is granted");
}
else {
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
}
return false;
} else {
return true;
}
} else {
return true;
}
}
我的另一个类在这里我称之为实用类是
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case Utility.MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if(userChoosenTask.equals("Take Photo"))
cameraIntent();
else if(userChoosenTask.equals("Choose from Library"))
galleryIntent();
//do something here
} else {
//code for deny
Toast.makeText(DetailMyColleague.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
break;
}
}
private void selectImage() {
final CharSequence[] items = { "Take Photo", "Choose from Library",
"Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(DetailMyColleague.this);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
boolean result=Utility.checkPermission(DetailMyColleague.this);
if (items[item].equals("Take Photo")) {
userChoosenTask ="Take Photo";
if(result)
cameraIntent();
} else if (items[item].equals("Choose from Library")) {
userChoosenTask ="Choose from Library";
if(result)
galleryIntent();
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
private void galleryIntent()
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, "Select File"),SELECT_FILE);
}
private void cameraIntent()
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_FILE)
onSelectFromGalleryResult(data);
else if (requestCode == REQUEST_CAMERA)
onCaptureImageResult(data);
}
}
private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
profilePic.setImageBitmap(thumbnail);
}
@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
Bitmap bm=null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
profilePic.setImageBitmap(bm);
}
我如何修改我的代码来每次显示此消息。
你不能。如果用户选中了“不要再问”,那么用户可以开通权限的唯一方法是通过设置应用程序。一旦用户选中“不要再问”并拒绝该权限,您就不能在该权限组中显示任何权限的权限对话框。
当权限已被“拒绝”且方法shouldShowRequestPermissionRegulation()返回false时,可以确定“不再显示”状态
当系统无法显示系统对话框(用户选择“接受”或“不再显示”)时,此方法可用于向用户显示其他信息
Deny >> shouldShowRequestPermissionRationale(permission) -> true
Don't ask again >> shouldShowRequestPermissionRationale(permission) -> false
Accept >> shouldShowRequestPermissionRationale(permission) -> false
如果您不能提供任何操作,而您请求的所有权限都被拒绝,并且其中至少有一个被“不要再问”复选框拒绝,最好的方法是将用户导航到系统中的应用程序设置。
以下是“接受”/“拒绝”/“不要再问”工作流程的解决方案。这里还有一个Jetpack活动结果API的例子。(请求多个权限,并在返回时导航到另一个活动)
代码注释:
注:代码并不完美,但有助于理解
碎片
class Fragment_ActiityResultAPI_RequestMultiplePermissions : Fragment(){
val actionPermLocation = {
tvPerm1.text = "GRANTED";
tvPerm1.setTextColor(Color.GREEN)
}
val actionPermReadExt = {
tvPerm2.text = "GRANTED";
tvPerm2.setTextColor(Color.GREEN)
}
val permissionsAll = mutableMapOf(
Manifest.permission.ACCESS_FINE_LOCATION to actionPermLocation,
Manifest.permission.READ_EXTERNAL_STORAGE to actionPermReadExt,
)
private val arcRequestPermissions = registerForActivityResult(RequestMultiplePermissions()){ perms ->
perms.entries.forEach {
if(it.value){
permissionsAll[it.key]?.invoke()
}
}
}
private val arcNavigateToSettings = registerForActivityResult(StartSettingsActivityContract()) {
arcRequestPermissions.launch(permissionsAll.keys.toTypedArray())
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_layout, container, false)
}
override fun onViewCreated(layoutView: View, savedInstanceState: Bundle?) {
super.onViewCreated(layoutView, savedInstanceState)
// CHECK PERMISSIONS AT FRAGMENT START
arcRequestPermissions.launch(permissionsAll.keys.toTypedArray())
btn.setOnClickListener {
// CHECK PERMISSION AT BUTTON CLICK
processPermission()
}
}
private fun processPermission() {
var atLeastOnePermDenied = false
var atLeastOnePermAsDontAskAgain = false
permissionsAll.keys.toTypedArray().forEach {
atLeastOnePermDenied = atLeastOnePermDenied || checkPermDenied(it)
atLeastOnePermAsDontAskAgain = atLeastOnePermAsDontAskAgain || checkPermDontAskAgain(it)
}
if(atLeastOnePermAsDontAskAgain){
showAlertNavigateToAppSettings()
return
}
if(atLeastOnePermDenied){
arcRequestPermissions.launch(permissionsAll.keys.toTypedArray())
return
}
Utils.toast(requireContext(), ">>> Execute your target action!! <<<")
}
private fun checkPermDenied(perm: String): Boolean {
return (ActivityCompat.checkSelfPermission(requireContext(), perm)
==
PackageManager.PERMISSION_DENIED)
}
private fun checkPermDontAskAgain(perm: String): Boolean {
return checkPermDenied(perm) && !shouldShowRequestPermissionRationale(perm)
}
private fun showAlertNavigateToAppSettings() {
val builder = AlertDialog.Builder(requireContext())
builder.setMessage("You have to grant permissions for action")
builder.setPositiveButton("Go to Settings") { dialog, which -> // Do nothing but close the dialog
arcNavigateToSettings.launch(1)
}
builder.setNegativeButton("Cancel") { dialog, which -> // Do nothing
dialog.dismiss()
}
val alert = builder.create()
alert.show()
}
class StartSettingsActivityContract : ActivityResultContract<Int, String?>() {
override fun createIntent(context: Context, input: Int): Intent {
return Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
val uri = Uri.fromParts("package", context.packageName, null)
this.data = uri
}
}
override fun parseResult(resultCode: Int, intent: Intent?): String? {
return ""
}
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="LOCATION: "
android:textColor="@android:color/darker_gray"/>
<TextView
android:id="@+id/tvPerm1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Denied"
android:textColor="@android:color/darker_gray"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="READ_STORAGE: "
android:textColor="@android:color/darker_gray"/>
<TextView
android:id="@+id/tvPerm2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Denied"
android:textColor="@android:color/darker_gray"/>
</TableRow>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_gravity="center_horizontal"
android:text="Launch a Camera"/>
</FrameLayout>
</LinearLayout>
建筑格拉德尔
implementation 'androidx.activity:activity-ktx:1.2.1'
implementation 'androidx.fragment:fragment:1.3.0-alpha05'
一旦他们说“不再询问”,您就不能显示相同的请求消息,但您可以检查权限,如果拒绝,则显示一个自定义对话框,将他们引导到设置页面,目的是:
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
如果权限检查失败:
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// show a dialog
new AlertDialog.Builder(this).setMessage("You need to enable permissions to use this feature").setPositiveButton("Go to settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// navigate to settings
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
}
}).setNegativeButton("Go back", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// leave?
MyActivity.this.onBackPressed();
}
}).show();
}
在我的应用程序中,我创建了一个类名untitility。在这个类名中,我从代码中获取read_external_storage的权限。但问题是,当我在应用程序上单击“拒绝”时,我找不到警报通知来再次设置“允许”。一开始我就有“不再问”的选项。一旦我点击它,对话框就会消失。现在运行应用程序后,如果我点击拒绝,我再也找不到对话框消息,使它再次允许。我如何修改我的代码,以显示这个消息每次。 我的另一个类
但是,无论我拒绝多少次权限,我都不能在权限对话框中看到“永远不要再问”复选框,而且ShowRequestPermissionRationale总是返回false。 有什么不对劲吗?
我想知道“永远不要再问”复选框布尔标志存储在哪里,如何清除它的值?不一定是通过编程,而是手动--通过设置、命令或某种工具。尝试清除应用程序数据,卸载,卸载和清除,尝试手动切换权限开/关来回,甚至尝试为模拟器设置一个更新的Marshmallow图像,但没有运气!
我想知道“不再问一遍”复选框布尔标志存储在哪里,以及如何清除它的值?不一定是编程的,而是手动的-通过设置,命令或一些工具。尝试清除应用程序数据,卸载,卸载和清除,尝试手动切换权限来回,甚至尝试为模拟器设置一个更新的Marshmallow映像,但没有运气!
我是android的新手,不知道我做错了什么,这个类得到了经度和纬度并发送到另一个类,代码抛出了一个错误请告诉我该怎么做怎么做: public void onRequestPermissionsResult出错(int requestCode,String[]permissions,int[]grantResults
根据以下内容:http://developer.android.com/preview/features/runtime-permissions.html#编码应用程序可以检查运行时权限,并在尚未授予权限的情况下请求权限。这时将显示以下对话框: 如果用户拒绝了一个重要的权限,imo一个应用程序应该显示一个解释,为什么需要这个权限,以及拒绝有什么影响。该对话框有两个选项: 重试(再次请求权限) 拒绝