不能再通过createPackageContext来访问另外一个应用的资源(Sharepreference)

以前我们可以通过createPackageContext来访问另外一个应用的资源(Sharepreference),但Android 7.0以后为了安全性,丢弃了这种方式了
PS:
/**
     * File creation mode: allow all other applications to have read access to
     * the created file.
     * <p>
     * As of {@link android.os.Build.VERSION_CODES#N} attempting to use this
     * mode will throw a {@link SecurityException}.
     *
     * @deprecated Creating world-readable files is very dangerous, and likely
     *             to cause security holes in applications. It is strongly
     *             discouraged; instead, applications should use more formal
     *             mechanism for interactions such as {@link ContentProvider},
     *             {@link BroadcastReceiver}, and {@link android.app.Service}.
     *             There are no guarantees that this access mode will remain on
     *             a file, such as when it goes through a backup and restore.
     * @see android.support.v4.content.FileProvider
     * @see Intent#FLAG_GRANT_WRITE_URI_PERMISSION
     */
    @Deprecated
    public static final int MODE_WORLD_READABLE = 0x0001;

若再尝试使用MODE_WORLD_READABLE 这种方式来建立SharePreference 会报SecurityException。

01-08 05:32:21.465 16339 16339 E AndroidRuntime: Caused by: java.lang.SecurityException: MODE_WORLD_READABLE no longer supported
01-08 05:32:21.465 16339 16339 E AndroidRuntime:        at android.app.ContextImpl.checkMode(ContextImpl.java:2134)
01-08 05:32:21.465 16339 16339 E AndroidRuntime:        at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:354)
01-08 05:32:21.465 16339 16339 E AndroidRuntime:        at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:349)
01-08 05:32:21.465 16339 16339 E AndroidRuntime:        at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:164)
01-08 05:32:21.465 16339 16339 E AndroidRuntime:        at com.android.deskclock.Utils.getCESharedPreferences(Utils.java:778)
01-08 05:32:21.465 16339 16339 E AndroidRuntime:        at com.android.deskclock.settings.DefaultAlarmToneDialog.<init>(DefaultAlarmToneDialog.java:74)

猜你喜欢

转载自aijiawang-126-com.iteye.com/blog/2399714