某个AndroidProject项目在Android5.0系统运行是OK的,在Android7.0以上版本运行,报以下错误❌
2019-01-02 15:33:39.996 17067-17067/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: liudeli.music2:remote, PID: 17067
java.lang.RuntimeException: Unable to create service liudeli.music2.service.MediaPlaybackService: java.lang.SecurityException: MODE_WORLD_READABLE no longer supported
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3226)
at android.app.ActivityThread.-wrap5(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1586)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6186)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
Caused by: java.lang.SecurityException: MODE_WORLD_READABLE no longer supported
at android.app.ContextImpl.checkMode(ContextImpl.java:2134)
at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:354)
at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:349)
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:164)
at liudeli.music2.service.MediaPlaybackService.onCreate(MediaPlaybackService.java:345)
解决方法:
// mPreferences = getSharedPreferences("Music", MODE_WORLD_READABLE | MODE_WORLD_WRITEABLE); mPreferences = getSharedPreferences("music", MODE_PRIVATE);
把 MODE_WORLD_READABLE 更换成 MODE_PRIVATE 即可,因为MODE_WORLD_READABLE 模式已经被废弃