You can no longer access resources (Sharepreference) of another application through createPackageContext

In the past, we could access the resources (Sharepreference) of another application through createPackageContext, but after Android 7.0, this method was discarded for security
:
/**
     * 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)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326071609&siteId=291194637