【发布时间】:2017-01-08 15:41:46
【问题描述】:
我的项目有 3 种不同的活动。第一个活动包含所有权限请求。应用程序已授予所有权限,但仍无法在 sdcard 上写入文件。如果我关闭应用程序并再次启动它,那么我可以在 sdcard 上写入文件。是否有多个活动的更新权限。
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();
}
}
以上是我的许可请求活动。我很抱歉英语不好。
【问题讨论】:
-
用户批准后,权限即授予整个应用程序。如果您仍然无法写入外部存储,则可能是其他原因,或者权限实际上并未获得批准。
-
我认为我的许可请求工作正常,但它没有立即授予这些权限。如果我去应用程序设置,它会显示该应用程序具有权限,但仍然无法创建文件或文件夹。如果应用重新启动,则没有问题。
-
Android 应用权限使用 UID 权限处理。权限授予您的应用程序运行的 UID。重新启动您的应用不会更改此 UID 或其权限的任何内容。
-
Joonas,你解决了这个问题吗?我有完全相同的问题。尽管 ContextCompat.checkSelfPermission(this,android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) 返回 true,但我的应用 New Playlist Manager 无法在外部 sdcard 上创建新文件夹。
-
没有。我认为这是 Android 中的问题/错误。我的临时修复:告诉用户再次启动应用程序,然后我强制停止应用程序的代码。问题仅适用于 Android 6.0 及更高版本。
标签: android permissions android-6.0-marshmallow