27

该异常仅发生在 Android 7.0 Nougat(模拟器)设备中。

java.lang.SecurityException:不再支持 MODE_WORLD_READABLE

我的代码:

public void SessionMaintainence(Context context) {
    this.context = context;
    preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_WORLD_READABLE);
    editor = preferences.edit();
    editor.commit();
}

日志猫:

> E/AndroidRuntime: FATAL EXCEPTION: main
>                                                  Process: burpp.av.feedback, PID: 2796
>                                                  java.lang.RuntimeException: Unable to create application
> burpp.av.feedback.FeedbackApplication: java.lang.SecurityException:
> MODE_WORLD_READABLE no longer supported
>                                                      at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5364)
>                                                      at android.app.ActivityThread.-wrap2(ActivityThread.java)
>                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
>                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
>                                                      at android.os.Looper.loop(Looper.java:154)
>                                                      at android.app.ActivityThread.main(ActivityThread.java:6077)
>                                                      at java.lang.reflect.Method.invoke(Native Method)
>                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
>                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
>                                                   Caused by: java.lang.SecurityException: MODE_WORLD_READABLE no longer supported
>                                                      at android.app.ContextImpl.checkMode(ContextImpl.java:2162)
>                                                      at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:363)
>                                                      at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:358)
>                                                      at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:164)
>                                                      at burpp.av.feedback.support.SessionMaintainence.<init>(SessionMaintainence.java:63)
>                                                      at burpp.av.feedback.FeedbackApplication.onCreate(FeedbackApplication.java:43)
>                                                      at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1024)
4

1 回答 1

41

世界可读的文件可能是一个安全漏洞。所以android首先弃用它,然后完全删除它。MODE_WORLD_READABLE在 .之前的版本中已弃用Android M。但在Android N它不再被支持和抛出SecurityException。所以尝试不同的模式。我用过Context.MODE_PRIVATE,效果很好。

于 2016-11-28T04:26:39.643 回答