Skip to content

宿主和插件如何互通SharedPreferences? #206

Open
@icodeu

Description

@icodeu

宿主和插件的SharedPreferences保存在不同的目录中,那么host和plugin中的SP如何能够相互访问呢?

Activity

Xindouble

Xindouble commented on Jun 12, 2016

@Xindouble

只能将插件中需要的参数传递过来保存,或者将数据保存在SD卡中

jymot

jymot commented on Jun 12, 2016

@jymot

建议保存到一个地方,比如Host中,然后plugin使用SharedPreferences的时候,其实使用的都是Host的SharedPreferences

icodeu

icodeu commented on Jun 13, 2016

@icodeu
Author

@motcwang Host保存的时候是保存在/data/data/host/shared_prefs/xxx.xml文件中,而插件在保存的时候在 /data/data/host/Plugin/pluginname/data/xxx.xml 都是在不同的目录,怎么可以相互共享呢?我还是有点不明白,还麻烦多多指教~

jymot

jymot commented on Jun 13, 2016

@jymot

@icodeu 比如可以让Host提供API,Plugin在使用SharedPreferences的时候,使用Host提供的API方法,这样就可以使Host和Plugin实现共享了。Host提高API的方法可以用AIDL,Service等实现,但是建议用AIDL。

weijianfeng

weijianfeng commented on Sep 1, 2016

@weijianfeng

下面这种简单粗暴的方式,是不是不能用了?

        try {
            Context otherAppsContext = createPackageContext("HostPackageName", Context.CONTEXT_IGNORE_SECURITY);
            SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences("test", Context.MODE_WORLD_READABLE);
            if (sharedPreferences != null) {
                String str = sharedPreferences.getString("key",null);
                Toast.makeText(getApplicationContext(), "result: " + str, Toast.LENGTH_SHORT).show();
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
cmzy

cmzy commented on Sep 1, 2016

@cmzy
Member

可以的。


Andy Zhang

Engineering Director (Beijing.China)
E-mail/Gtalk/QQ/MSN: zhangyong232@gmail.com
zy@mappn.com

2016-09-01 19:47 GMT+08:00 Wei Jianfeng notifications@github.com:

下面这种简单粗暴的方式,是不是不能用了?

    try {
        Context otherAppsContext = createPackageContext("HostPackageName", Context.CONTEXT_IGNORE_SECURITY);
        SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences("test", Context.MODE_WORLD_READABLE);
        if (sharedPreferences != null) {
            String str = sharedPreferences.getString("key",null);
            Toast.makeText(getApplicationContext(), "result: " + str, Toast.LENGTH_SHORT).show();
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#206 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAdmPy0Zt21-P6IQ8oX_yVRtPuaM_kIQks5qlrttgaJpZM4IuV3j
.

smartyuge

smartyuge commented on Sep 2, 2016

@smartyuge
Contributor

这种方式,目前可用。可以解决pluginhost和plugin apk数据不共享问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @cmzy@jymot@weijianfeng@smartyuge@icodeu

        Issue actions

          宿主和插件如何互通SharedPreferences? · Issue #206 · DroidPluginTeam/DroidPlugin