在 2 个 android 应用程序之间共享 SQLite 数据库? [英] Share SQLite database between 2 android apps?

查看:24
本文介绍了在 2 个 android 应用程序之间共享 SQLite 数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 2 个应用程序之间共享一个数据库.我知道数据库将在/data/data/MY_PACKAGE/databases/上创建.由于包名称不同,当我在任一应用程序上创建数据库时,是否可以定义一个包名称的路径?谢谢.

I need to share a single database between 2 apps. I know that the database will be created on /data/data/MY_PACKAGE/databases/ . Since the packages names are different is it possible to define the path to one package name when I create the database on either app? Thanks.

推荐答案

UPDATE:下面描述的方法依赖于 android:sharedUserId,从 API 级别 29 (Android 10) 开始弃用.

UPDATE: The method described below relies on android:sharedUserId, deprecated as of API level 29 (Android 10).

您当然可以在 2 个应用之间共享一个数据库.

You certainly can share a single database between 2 apps.

为了在应用之间共享数据(前提是它们由同一发布者发布),您需要在两个应用的 AndroidManifest.xml 中指定共享用户 ID.

In order to share data between apps (provided they are issued by the same publisher) you will need to specify a shared user id in the AndroidManifest.xml of both apps.

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:sharedUserId="my.app" ... >

(未公开,但共享用户ID必须是至少有一个点分隔符的字符串)

(It's undocumented, but the shared user id needs to be a string with at least one dot separator)

剩下的就很简单了,你不需要弄乱数据库路径.只需在两个应用程序中使用相同的 DBAdapter.在托管数据库的应用程序中,使用本机上下文调用 DBAdapter.

The rest is easy, and you don't need to mess around with the database path. Just use the same DBAdapter in both apps. In the app that hosts the database, call the DBAdapter with the native context.

DBadapter hostDBAdapter = new DbAdapter(getApplicationContext());
performerDBadapter.open();

在第二个应用程序中,使用数据库托管应用程序的上下文访问数据库.
首先,定义共享上下文:

In the second app, access the database with the context of the database hosting app.
First, define the shared context:

Context sharedContext = null;
    try {
        sharedContext = this.createPackageContext("replace.with.host.package.name", Context.CONTEXT_INCLUDE_CODE);
        if (sharedContext == null) {
            return;
        }
    } catch (Exception e) {
        String error = e.getMessage(); 
        return;
        }   

然后用共享上下文打开DBAdapter:

Then open the DBAdapter with the shared context:

DbAdapter sharedDBadapter = new PerformerDbAdapter(sharedContext);
sharedDBadapter.open();

最后一点,如果您的数据库在清单中设置共享用户 ID 之前存在,您将需要在物理设备上卸载/重新安装应用程序,以免您将自己锁定在数据库之外(sqlite 错误 14).另一方面,模拟器可能被证明更宽容.最重要的是,如果您的应用发布在 Android 市场上,事后设置共享用户 ID 将不起作用.

As a final note, if your database exists previous to setting the shared user id in the manifest, you will need to uninstall/reinstall the apps on a physical device, lest you will lock yourself out of your database (sqlite error 14). The emulator, on the other hand, might prove to be more forgiving. Bottom line, if your apps are published on the Android market, setting a shared user id in an afterthought will not work.

希望这会有所帮助.

这篇关于在 2 个 android 应用程序之间共享 SQLite 数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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