在Lollipop中,下载功能在我的应用程序中运行良好,但是当我升级到Marshmallow时,我的应用程序崩溃并在我尝试从互联网下载到SD卡时出现此错误:
Neither user nor current process has android.permission.WRITE_EXTERNAL_STORAGE
Run Code Online (Sandbox Code Playgroud)
它抱怨这行代码:
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
Run Code Online (Sandbox Code Playgroud)
我在应用程序外的清单中拥有权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
Run Code Online (Sandbox Code Playgroud)
我清理并重建了项目,但它仍然崩溃.
假设您有这样的方法:
public boolean saveFile (Url url, String content) {
// save the file, this can be done a lot of different ways, but
// basically the point is...
return save_was_successful;
}
Run Code Online (Sandbox Code Playgroud)
在整个应用程序中,如果要将文件保存到外部存储,则执行以下操作:
if (saveFile(external_storage_url, "this is a test")) {
// yay, success!
} else { // notify the user something was wrong or handle the error }
Run Code Online (Sandbox Code Playgroud)
这是一个简单的例子,所以不要对我的情况大概阻塞UI,妥善处理异常,等等.如果你不喜欢的文件保存得到的,你能想象一个getContact()
或getPhoneState()
或什么的.重点是它是一个需要许可的操作,它返回一些值并在整个应用程序中使用它.
在Android <= Lollipop中,如果用户已经安装并同意授予android.permission.WRITE_EXTERNAL_STORAGE
或其他什么,那么一切都会好的.
但是在新的Marshmallow(API 23)运行时权限模型中,在将文件保存到外部存储之前,您应该(1)检查是否已授予权限. 如果没有,可能(2)显示请求的理由(如果系统认为这是一个好主意)与吐司或其他什么,以及(3)要求用户通过对话授予权限,然后基本上坐下来等待打回来...
(所以你的应用程序坐在那里,等待...)
(4)当用户最终响应对话框时,onRequestPermissionsResult() …
我正在使用启动时启动并开始检查位置更新的服务.一旦我拒绝在权限弹出窗口上进行位置访问,感谢Android M我的服务在手机启动后崩溃了.
因为我已经没有活动在这种情况下,调用requestPermissions()
返回一个ClassCastException
作为我的服务Context
能不能被转换为一个活动.
我的方法调用:
ActivityCompat.requestPermissions((Activity) mContext, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_COARSE_LOCATION);
Run Code Online (Sandbox Code Playgroud)
到目前为止是否有任何解决方案或者我是否必须撤销服务权限以免在这种状态下运行.
我有一个应用程序,需要找到用户位置,并在各个类中提取位置,所以我写了一个单独的类(非活动类),使用位置服务获取用户位置,它在Android M下工作正常,但需要运行Android M中的时间权限,我想检查我的Location类中的权限,我知道如何检查它们但我不能在我的Location类中使用onRequestPermissionsResult方法,因为我的Location类不会从任何活动扩展.
那么我应该做些什么呢?任何帮助/线索表示感谢提前感谢
我需要从我的应用程序中的几个非UI组件访问位置.由于应用程序可能没有所需的权限,因此我在如何从Android Marshmallow中的服务请求权限的答案中实现了一个解决方案:如果应用程序没有权限,则会显示通知.点击该通知将使用户变为空Activity
,这将在顶部显示运行时权限对话框.
但是,我现在遇到以下问题:如果用户在没有做出选择的情况下解除该对话框(例如通过点击Home)并稍后调出我的应用程序的任何其他活动,则会重新显示运行时权限对话框而不是所需的活动.
这是我的通知代码:
Intent permIntent = new Intent(context, PermissionRequestActivity.class);
permIntent.putExtra(Const.KEY_RESULT_RECEIVER, resultReceiver);
permIntent.putExtra(Const.KEY_PERMISSIONS, permissions);
permIntent.putExtra(Const.KEY_REQUEST_CODE, requestCode);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(PermissionRequestActivity.class);
stackBuilder.addNextIntent(permIntent);
PendingIntent permPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(notificationIcon)
.setContentTitle(notificationTitle)
.setContentText(notificationText)
.setOngoing(true)
.setAutoCancel(true)
.setWhen(0)
.setContentIntent(permPendingIntent)
.setStyle(null);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(requestCode, builder.build());
Run Code Online (Sandbox Code Playgroud)
有没有办法可以在Activity
停止时中止运行时权限对话框?