如何将共享首选项从即时应用转移到完整应用 [英] How to transfer the shared prefs from Instant app to full app

查看:29
本文介绍了如何将共享首选项从即时应用转移到完整应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我们可以使用 Google Instant 的 Storage api 将数据从免安装应用传输到完整应用,如前所述 此处.

I know we can transfer the data from instant app to the full app using the Storage api of Google Instant as mentioned here.

对于运行低于 Oreo 的操作系统版本的设备,我尝试按如下方式读取数据:

For devices running OS version less than Oreo, I am trying to read the data as follows:

 public void getInstantAppData(final Activity activity, final InstantAppDataListener listener) {
    InstantApps.getInstantAppsClient(activity)
            .getInstantAppData()
            .addOnCompleteListener(new OnCompleteListener<ParcelFileDescriptor>() {
                @Override
                public void onComplete(@NonNull Task<ParcelFileDescriptor> task) {

                    try {
                        FileInputStream inputStream = new FileInputStream(task.getResult().getFileDescriptor());
                        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
                        ZipInputStream zipInputStream = new ZipInputStream(bufferedInputStream);

                        ZipEntry zipEntry;

                        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
                            Log.i("Instant-app", zipEntry.getName());
                            if (zipEntry.getName().equals("shared_prefs/")) {
                                extractSharedPrefsFromZip(activity, zipEntry);
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
}

private void extractSharedPrefsFromZip(Activity activity, ZipEntry zipEntry) throws IOException {
    File file = new File(activity.getApplicationContext().getFilesDir() + "/shared_prefs.vlp");
    mkdirs(file);
    FileInputStream fis = new FileInputStream(zipEntry.getName());

    BufferedInputStream bis = new BufferedInputStream(fis);
    ZipInputStream stream = new ZipInputStream(bis);
    byte[] buffer = new byte[2048];

    FileOutputStream fos = new FileOutputStream(file);
    BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);

    int length;
    while ((length = stream.read(buffer)) > 0) {
        bos.write(buffer, 0, length);
    }
}

但我收到错误 Method throw 'java.io.FileNotFoundException' 异常. 基本上,当我尝试读取 shared_pref 文件时,它无法找到它.文件的全名是什么,是否有更好的方法将我的共享偏好数据从免安装应用传输到安装应用.

But I am getting an error Method threw 'java.io.FileNotFoundException' exception. Basically when I am trying to read the shared_pref file it is not able to locate it. What is the full name of the file and is there any better way to transfer my shared pref data from instant app to installed app.

推荐答案

在花了几个小时之后,我终于能够让它工作,但后来我发现了一个更好、更简单的方法来做到这一点.Google 还有一个 cookie api,可用于在用户升级时将免安装应用中的数据共享到完整应用.

After spending several hours I was able to make it work but then I found out a much better and easier way to do this. Google also has a cookies api which can be used to share the data from instant app to your full app when user upgrades.

文档:https://developers.google.com/android/reference/com/google/android/gms/instantapps/PackageManagerCompat#setInstantAppCookie(byte%5B%5D)

示例:https://github.com/googlesamples/android-instant-apps/tree/master/cookie-api

我更喜欢这个,因为它更干净、更容易实现,但最重要的是你不必将可安装应用程序的目标沙盒版本增加到 2,如果你使用 Storage API,这是必需的.它适用于操作系统版本大于或等于 8 的设备以及操作系统版本小于 8 的设备.

I preferred this because it is much cleaner, easy to implement but the most important thing is that you don't have to increase the target sandbox version of your installable app to 2, which is required if you use the Storage API. It works in devices with OS version greater than or equal to 8 as well as with devices with OS version less than 8.

希望这对某人有所帮助.

Hope this helps somebody.

这篇关于如何将共享首选项从即时应用转移到完整应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆