权限拒绝:写入设置需要android.permission.WRITE_SETTINGS

时间:2016-01-19 13:42:48

标签: android permissions

对于数量非常有限的用户,我的应用程序崩溃了以下内容:

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说:

  

有一些权限不像普通人那样行事   危险的权限。 SYSTEM_ALERT_WINDOW和WRITE_SETTINGS是   特别敏感,所以大多数应用程序不应该使用它们。如果一个应用程序   需要其中一个权限,它必须在中声明权限   显示,并发送请求用户授权的意图。该   系统通过显示详细的管理屏幕来响应意图   给用户。

我想尽我所能避免在我的应用中获得此权限,因为它显然是“危险的”#34;并且必须在运行时请求此权限才足以吓跑我的应用程序非锡帽子用户。我真的不需要写信给设置......我写信给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
}

1 个答案:

答案 0 :(得分:0)

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

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
}

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

所以,由于某种原因,我仍然不明白,无论是试图获取drawable,还是从drawable获取位图,这都会以某种方式导致Settings$System.putIntSettings$System.putIntForUser以及{{ 1}}日志条目,这反过来导致Settings$System.putStringForUser错误(尝试未经许可写入设置)。

This post沿着类似的路线,报告错误似乎与“系统壁纸为空”的情况有关。当我尝试使用SecurityException: Permission denial做一些不可言说的事情时,为什么不只有NullPointerException,而是在显然试图篡改设置后出现了严重的权限错误,我不知道。