【问题标题】:How to transfer the shared prefs from Instant app to full app如何将共享首选项从即时应用程序转移到完整应用程序
【发布时间】:2019-01-10 15:42:16
【问题描述】:

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

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

 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 threw 'java.io.FileNotFoundException' exception. 基本上当我试图读取 shared_pref 文件时它无法找到它。文件的全名是什么?有没有更好的方法将我的共享首选项数据从即时应用程序传输到已安装的应用程序。

【问题讨论】:

  • 文件的全名是您在使用Context.getSharedPreferences 创建文件时选择的名称。我不确定,但我认为您的代码可能正在尝试读取目录 ZipEntry。在检测到目录之后和extractSharedPrefsFromZip 之前尝试移动到下一个条目,如果它不起作用,您可以发布异常的完整堆栈跟踪吗?
  • stackoverflow.com/a/45315101/6668797,如果只是共享首选项,使用cookie api会更容易,但如果你打算使用ZIP方法,我稍后会仔细研究。

标签: android zip android-instant-apps


【解决方案1】:

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

文档: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 的设备。

希望这对某人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多