gpt4 book ai didi

android - 权限拒绝 : writing to settings requires android. permission.WRITE_SETTINGS

转载 作者:太空狗 更新时间:2023-10-29 14:51:31 46 4
gpt4 key购买 nike

对于极少数用户,我的应用程序因以下原因崩溃:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cloud3squared.meteogram.pro/com.cloud3squared.meteogram.MeteogramWidgetConfigureActivity}:
java.lang.SecurityException: Permission denial: writing to settings requires android.permission.WRITE_SETTINGS
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
Caused by: java.lang.SecurityException: Permission denial: writing to settings requires android.permission.WRITE_SETTINGS
at android.os.Parcel.readException(Parcel.java:1540)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
at android.content.ContentProviderProxy.call(ContentProviderNative.java:643)
at android.provider.Settings$NameValueCache.putStringForUser(Settings.java:1204)
at android.provider.Settings$System.putStringForUser(Settings.java:1505)
at android.provider.Settings$System.putIntForUser(Settings.java:1616)
at android.provider.Settings$System.putInt(Settings.java:1607)
at android.app.WallpaperManager$Globals.getDefaultWallpaperLocked(WallpaperManager.java:483)
at android.app.WallpaperManager$Globals.peekWallpaperBitmap(WallpaperManager.java:303)
at android.app.WallpaperManager.getDrawable(WallpaperManager.java:550)
at com.cloud3squared.meteogram.ax.c(Unknown Source)
at com.cloud3squared.meteogram.MeteogramWidgetConfigureActivity.a(Unknown Source)
at com.cloud3squared.meteogram.MeteogramWidgetConfigureActivity.c(Unknown Source)
at com.cloud3squared.meteogram.MeteogramWidgetConfigureActivity.onCreate(Unknown Source)
at android.app.Activity.performCreate(Activity.java:6288)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
... 10 more

虽然崩溃报告中给出了该问题的解决方案(只需将 android.permission.WRITE_SETTINGS 添加到 Manifest),但我需要这样做让我很困扰,因为我的应用程序并不知道写入设置,绝大多数用户没有权限没有任何问题。

这特别令人不安,因为 the docs说:

There are a couple of permissions that don't behave like normal and dangerous permissions. SYSTEM_ALERT_WINDOW and WRITE_SETTINGS are particularly sensitive, so most apps should not use them. If an app needs one of these permissions, it must declare the permission in the manifest, and send an intent requesting the user's authorization. The system responds to the intent by showing a detailed management screen to the user.

我想尽我所能避免在我的应用程序中获得此权限,因为它显然是“危险的”,并且必须在运行时请求此权限足以吓跑即使是非锡帽用户。我真的不需要写设置...我写给SharedPreferences仅此而已。

那么有没有办法在不需要权限的情况下避免这个问题呢?

编辑以根据 comment below 添加代码(此函数是从我的 Activity 调用的):

static String getAverageWallpaperColour(Context context) {
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
Bitmap bmp = ((BitmapDrawable)wallpaperDrawable).getBitmap();
int color = getAverageColour(bmp); // details of function not relevant... just reads the bmp to work out an average colour
return colorToHexString(color); // details of function not relevant... just converts into color to String in particular format
}

最佳答案

我现在确定问题出在我在编辑中添加到问题中的代码 fragment 中:

static String getAverageWallpaperColour(Context context) {
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
Bitmap bmp = ((BitmapDrawable)wallpaperDrawable).getBitmap();
int color = getAverageColour(bmp); // details of function not relevant... just reads the bmp to work out an average colour
return colorToHexString(color); // details of function not relevant... just converts into color to String in particular format
}

基本上,应用程序崩溃的用户告诉我,他们的设备上有动态壁纸,而不是传统的静态壁纸。

因此,出于某种原因我仍然不明白,无论是尝试获取可绘制对象,还是从可绘制对象获取位图,这都会以某种方式导致 Settings$System.putIntSettings$System.putIntForUserSettings$System.putStringForUser 日志条目,进而导致 SecurityException: Permission denial 错误(尝试写入未经许可的设置)。

This post遵循类似的思路,报告错误似乎与“系统墙纸为空”的情况有关。这就是为什么当我尝试用 null 做一些无法形容的事情时,不仅出现 NullPointerException,而且在显然试图篡改设置后出现可怕的权限错误,我不知道。

关于android - 权限拒绝 : writing to settings requires android. permission.WRITE_SETTINGS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34878382/

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