Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
icodeu opened this issue Jun 5, 2016 · 7 comments
Open

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

icodeu opened this issue Jun 5, 2016 · 7 comments

Comments

@icodeu
Copy link

icodeu commented Jun 5, 2016

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

@Xindouble
Copy link

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

@jymot
Copy link

jymot commented Jun 12, 2016

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

@icodeu
Copy link
Author

icodeu commented Jun 13, 2016

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

@jymot
Copy link

jymot commented Jun 13, 2016

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

@weijianfeng
Copy link

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

        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
Copy link
Member

cmzy commented Sep 1, 2016

可以的。


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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants