“你无法更改私人安全设置” - 如何在Android 6中切换铃声振动?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (216)

由于Android 6 Marshmallow在权限方面的最近更改,以下代码不再有效并引发异常:

Settings.System.putInt(getContentResolver(), "vibrate_when_ringing", 0);

甚至在授予WRITE_SETTINGS应用程序许可权后,仍会发生这种情况:

Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
Uri uri = Uri.fromParts("package", getActivity().getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, WRITE_SETTINGS_PERMISSION_REQUEST_CODE);
//at this point Settings activity is opened and user is prompted for permission

...

if (Settings.System.canWrite(this)) { //now returns true, because permissions were granted
    Settings.System.putInt(getContentResolver(), "vibrate_when_ringing", 0); //still crashes
} else {
    Log.w(LOG_TAG, "No permission to write settings.");
}

全堆栈跟踪:

10-12 02:26:37.728 1927-2765/? E/DatabaseUtils: Writing exception to parcel
10-12 02:26:37.728 1927-2765/? E/DatabaseUtils: java.lang.IllegalArgumentException: You cannot change private secure settings.
10-12 02:26:37.728 1927-2765/? E/DatabaseUtils:     at com.android.providers.settings.SettingsProvider.warnOrThrowForUndesiredSecureSettingsMutationForTargetSdk(SettingsProvider.java:1173)
10-12 02:26:37.728 1927-2765/? E/DatabaseUtils:     at com.android.providers.settings.SettingsProvider.enforceRestrictedSystemSettingsMutationForCallingPackage(SettingsProvider.java:1030)
10-12 02:26:37.728 1927-2765/? E/DatabaseUtils:     at com.android.providers.settings.SettingsProvider.mutateSystemSetting(SettingsProvider.java:906)
10-12 02:26:37.728 1927-2765/? E/DatabaseUtils:     at com.android.providers.settings.SettingsProvider.insertSystemSetting(SettingsProvider.java:874)
10-12 02:26:37.728 1927-2765/? E/DatabaseUtils:     at com.android.providers.settings.SettingsProvider.call(SettingsProvider.java:257)
10-12 02:26:37.728 1927-2765/? E/DatabaseUtils:     at android.content.ContentProvider$Transport.call(ContentProvider.java:398)
10-12 02:26:37.728 1927-2765/? E/DatabaseUtils:     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:283)
10-12 02:26:37.728 1927-2765/? E/DatabaseUtils:     at android.os.Binder.execTransact(Binder.java:453)
10-12 02:26:37.729 10084-11411/<package_name> E/AndroidRuntime: FATAL EXCEPTION: IntentService[DetectedActivityIntentService]
10-12 02:26:37.729 10084-11411/<package_name> E/AndroidRuntime: Process: <package_name>, PID: 10084
10-12 02:26:37.729 10084-11411/<package_name> E/AndroidRuntime: java.lang.IllegalArgumentException: You cannot change private secure settings.
10-12 02:26:37.729 10084-11411/<package_name> E/AndroidRuntime:     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:165)
10-12 02:26:37.729 10084-11411/<package_name> E/AndroidRuntime:     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
10-12 02:26:37.729 10084-11411/<package_name> E/AndroidRuntime:     at android.content.ContentProviderProxy.call(ContentProviderNative.java:646)
10-12 02:26:37.729 10084-11411/<package_name> E/AndroidRuntime:     at android.provider.Settings$NameValueCache.putStringForUser(Settings.java:1322)
10-12 02:26:37.729 10084-11411/<package_name> E/AndroidRuntime:     at android.provider.Settings$System.putStringForUser(Settings.java:1671)
10-12 02:26:37.729 10084-11411/<package_name> E/AndroidRuntime:     at android.provider.Settings$System.putIntForUser(Settings.java:1776)
10-12 02:26:37.729 10084-11411/<package_name> E/AndroidRuntime:     at android.provider.Settings$System.putInt(Settings.java:1770)
10-12 02:26:37.729 10084-11411/<package_name> E/AndroidRuntime:     at <package_name>.DetectedActivityIntentService.activate(DetectedActivityIntentService.java:116)
10-12 02:26:37.729 10084-11411/<package_name> E/AndroidRuntime:     at <package_name>.DetectedActivityIntentService.onHandleIntent(DetectedActivityIntentService.java:94)
10-12 02:26:37.729 10084-11411/<package_name> E/AndroidRuntime:     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66)
10-12 02:26:37.729 10084-11411/<package_name> E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-12 02:26:37.729 10084-11411/<package_name> E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
10-12 02:26:37.729 10084-11411/<package_name> E/AndroidRuntime:     at android.os.HandlerThread.run(HandlerThread.java:61)
提问于
用户回答回答于

根据这张票,问题似乎在12月9日解决。问题将在未来版本中修复。

用户回答回答于

因为安全

正如我们所知,在Android 6.0(Marshmallow)中,Android增加了很多策略来控制安全性,提升效率,例如:

  • 运行时权限
  • 打盹模式(电源管理器)
  • 重新编写SettingsProvider
    • 将系统设置从db移动到xml
    • /data/data/com.android.providers.settings/../settings.db - > /data/system/users/userid/settings_[system|global|secure].xml

所以现在(API 22之后)

  • 不能写一些私人/危险的设置
  • 在移植到API级别22或更高版本时,您必须处理一些异常事件

PS

扫码关注云+社区

领取腾讯云代金券