gpt4 book ai didi

java - 根据Android权限是否禁用显示TextView?

转载 作者:行者123 更新时间:2023-12-01 10:23:25 27 4
gpt4 key购买 nike

我正在尝试显示一个 TextView,它仅在未启用特定 Android 权限(即 WRITE_EXTERNAL_STORAGE)时才可见。这个想法是向用户解释为什么他们需要启用它(如果他们第一次禁用它),然后让他们单击 TextView 再次启动权限请求。以下内容对我不起作用。我怎样才能做到这一点?我的 onCreate() 方法中有这个:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} else {
if (NetworkHelper.isOnline(this)) {
new AppendParseData().execute();
}
}

TextView checkFilePermission = (TextView) findViewById(R.id.checkFilePermission);

if (!checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
checkFilePermission.setVisibility(View.VISIBLE);
}

最佳答案

当您请求权限时尝试这种方式(我已经使用了READ_CONTACTS权限,您可以使用WRITE_EXTERNAL_STORAGE代替)

if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
requestContactsPermissions1();
} else {
setTextVisablity(false);
checkSimCard();
}

onRequestPermissionsResult

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
readContacts();
setTextVisablity(false);
} else {
// Permission Denied
setTextVisablity(true);
Toast.makeText(MainActivity.this, "READ_CONTACTS Denied", Toast.LENGTH_SHORT)
.show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}

最后setTextVisablity方法

public void setTextVisablity(boolean isShow) {
if (isShow) {
tv.setVisibility(View.VISIBLE);
} else {
tv.setVisibility(View.GONE);
}
}

关于java - 根据Android权限是否禁用显示TextView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35456665/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com