gpt4 book ai didi

已授予 Android 权限但仍被拒绝

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:13:39 24 4
gpt4 key购买 nike

我的项目有 3 个不同的 Activity 。第一个 Activity 包含所有权限请求。应用程序已授予所有权限,但仍无法在 SD 卡上写入文件。如果我关闭应用程序并再次启动它,那么我可以在 sdcard 上写入文件。无论如何,是否有多个 Activity 的更新权限。

public class PermissionActivity extends AppCompatActivity {
public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;
private static int TIME_OUT = 2000;
private String TAG = "tag";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_permission);

Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
this.getResources().updateConfiguration(config, null);

if(checkAndRequestPermissions()) {
// carry on the normal flow, as the case of permissions granted.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity

Intent i = new Intent(PermissionActivity.this, MainActivity.class);
startActivity(i);

// close this activity
finish();
}
}, TIME_OUT);
}
}

@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
Log.d(TAG, "Permission callback called-------");
switch (requestCode) {
case REQUEST_ID_MULTIPLE_PERMISSIONS: {

Map<String, Integer> perms = new HashMap<>();
// Initialize the map with both permissions
perms.put(android.Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
perms.put(android.Manifest.permission.WRITE_CALENDAR, PackageManager.PERMISSION_GRANTED);
perms.put(android.Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(android.Manifest.permission.ACCESS_COARSE_LOCATION, PackageManager.PERMISSION_GRANTED);
// Fill with actual results from user
if (grantResults.length > 0) {
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for both permissions
if (perms.get(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
&& perms.get(android.Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED
&& perms.get(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& perms.get(android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "all permission granted");

// process the normal flow
Intent i = new Intent(PermissionActivity.this, MainActivity.class);
startActivity(i);
finish();
//else any one or both the permissions are not granted
} else {
Log.d(G.mLogTag, "Some permissions are not granted ask again ");
if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
|| ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_CALENDAR)
|| ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
|| ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
showDialogOK("Service Permissions are required for this app",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
checkAndRequestPermissions();
break;
case DialogInterface.BUTTON_NEGATIVE:
// proceed with logic by disabling the related features or quit the app.
finish();
break;
}
}
});
}
else {
explain("You need to give some mandatory permissions to continue. Do you want to go to app settings?");
}
}
}
}
}
}

private boolean checkAndRequestPermissions(){
int writeStoragePermission = ContextCompat.checkSelfPermission(PermissionActivity.this,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
int writeCalenderPermission = ContextCompat.checkSelfPermission(this,
android.Manifest.permission.WRITE_CALENDAR);
int fineLocationPermission = ContextCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_FINE_LOCATION);
int coarseLocationPermission = ContextCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_COARSE_LOCATION);

List<String> listPermissionsNeeded = new ArrayList<>();
if (writeStoragePermission != PackageManager.PERMISSION_GRANTED)
listPermissionsNeeded.add(android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (writeCalenderPermission != PackageManager.PERMISSION_GRANTED)
listPermissionsNeeded.add(android.Manifest.permission.WRITE_CALENDAR);
if (fineLocationPermission != PackageManager.PERMISSION_GRANTED)
listPermissionsNeeded.add(android.Manifest.permission.ACCESS_FINE_LOCATION);
if (coarseLocationPermission != PackageManager.PERMISSION_GRANTED)
listPermissionsNeeded.add(android.Manifest.permission.ACCESS_COARSE_LOCATION);
if (!listPermissionsNeeded.isEmpty()){
ActivityCompat.requestPermissions(this, listPermissionsNeeded
.toArray(new String[listPermissionsNeeded.size()]),
REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}

private void showDialogOK(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", okListener)
.create()
.show();
}
private void explain(String msg){
final android.support.v7.app.AlertDialog.Builder dialog = new android.support.v7.app.AlertDialog.Builder(this);
dialog.setMessage(msg)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// permissionsclass.requestPermission(type,code);
startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.parse("package:fi.maxitors.mydiscgolfapp")));
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
finish();
}
});
dialog.show();
}

以上是我的许可请求 Activity 。很抱歉英语不好。

最佳答案

默认情况下,以 Android 10(API 级别 29)及更高版本为目标平台的应用会获得对外部存储设备或分区存储的分区访问权限。此类应用只能看到其应用特定的目录

  1. 临时修复-

    < list ...><应用程序 android:requestLegacyExternalStorage="true"... >...

  2. 永久修复-打开文件描述符

关于已授予 Android 权限但仍被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41534455/

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