当我的片段被夸大时,它会根据android 6.0+中的要求为权限dialog box
打开WRITE_EXTERNAL_STORAGE
。我已经按照每个教程进行了操作,但是在给出或拒绝许可时我的所有操作都没有执行。
我希望应用关闭,并在权限被拒绝时显示祝酒,这意味着重新打开应用时的permission would be asked again
。
片段开始:
public class SecondFragment extends Fragment {
View myView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.second_layout, container, false);
configureImageButton();
((MainActivity) getActivity()).setActionBarTitle("App");
if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
} else {
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
1);
}
}
onRequestPermissionsResult:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Snackbar.make(myView, "Permission Granted.", Snackbar.LENGTH_LONG).show();
} else if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED) {
Toast.makeText(getActivity(), "Reopen app and allow permission.", Toast.LENGTH_LONG).show();
getActivity().finish();
}
}
}
该应用确实授予/拒绝权限,但忽略了吐司或小吃吧。 所有建议表示赞赏。谢谢!
答案 0 :(得分:0)
里面片段有一些变化 请尝试下面的
private void checkLocationPermission() {
if (ContextCompat.checkSelfPermission(this.mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//Prompt the user once explanation has been shown
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_CODE);
} else {
// No explanation needed, we can request the permission.
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_CODE);
}
}
}
你案件中的MY_PERMISSIONS_REQUEST_CODE = 1
答案 1 :(得分:0)
(1)如果您的应用已被授予,则您无法通过此代码查看权限对话框。
if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED)
因此,请转到设置 - 应用程序 - 您的应用 - 并禁用已授予的权限,然后再次运行您的应用。
(2)你的onCreateView是否正确返回myView?在您的示例代码中,没有"返回myView;"
答案 2 :(得分:0)
public static final int RequestPermissionCodeStorage = 105; // Declare Global Variable
public boolean checkStoragePermission() {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int result = ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (result == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, RequestPermissionCodeStorage);
return false;
}
} else {
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case RequestPermissionCodeStorage: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
Toast.makeText(this, "Allow Permission of Storage", Toast.LENGTH_LONG).show();
finish();
}
return;
}
}
}