我知道我们可以使用 Google Instant 的存储 api 将数据从即时应用程序传输到完整应用程序,如上所述here https://developers.google.com/android/reference/com/google/android/gms/instantapps/InstantAppsClient#getInstantAppData().
对于运行操作系统版本低于 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 文件时,它无法找到它。文件的全名是什么?是否有更好的方法将我的共享偏好数据从即时应用程序传输到已安装的应用程序。
花了几个小时后,我终于成功了,但后来我发现了一种更好、更简单的方法来做到这一点。 Google 还有一个 cookies api,可用于在用户升级时将即时应用程序的数据共享到完整应用程序。
文档:https://developers.google.com/android/reference/com/google/android/gms/instantapps/PackageManagerCompat#setInstantAppCookie(byte%5B%5D) 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 https://github.com/googlesamples/android-instant-apps/tree/master/cookie-api
我更喜欢这个,因为它更干净,易于实现,但最重要的是,您不必将可安装应用程序的目标沙箱版本增加到 2,如果您使用存储 API,则需要将其增加到 2。它适用于操作系统版本大于或等于 8 的设备以及操作系统版本小于 8 的设备。
希望这对某人有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)