【问题标题】:Two Application's using sharedUserID - createPackageContext issue两个应用程序使用 sharedUserID - createPackageContext 问题
【发布时间】:2016-04-20 13:09:00
【问题描述】:

我有两个独立的应用程序。我希望 appB 能够访问 appA 代码中的数据库。我不想在这种情况下使用内容提供程序。

我已阅读有关此的 SO 帖子并按照说明进行操作 (Share SQLite database between 2 android apps?)。

我做了什么:

AppA 的 Android 清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.micode.primaryapplication">

    <application
        android:sharedUserId="com.micode.sharedid"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

AppB 的 Android 清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.micode.secondaryapplication">

    <application
        android:sharedUserId="com.micode.sharedid"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

AppA创建数据库,AppB需要访问数据库。

AppB中使用如下代码:

package com.micode.secondaryapplication;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    Button btnExit;
    TextView txtViewDisplay;
    Context sharedContext;

    SQLiteDatabase db = null;
    private static String DBNAME = "Contacts.db";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setupContext();
    }

    private void setupContext()
    {
        try {
            sharedContext = this.createPackageContext("com.micode.primaryapplication", Context.CONTEXT_INCLUDE_CODE);
            if (sharedContext == null) {
                return;
            }
        } catch (Exception e) {
            String error = e.getMessage();
            System.out.println(error);
            return;
        }
    }

我尝试使用完全相同的签名密钥构建两个应用程序,即构建/生成签名的 APK,并在安装之前卸载了任何以前的实例。

我也在尝试在调试模式下运行它,即在调试模式下安装 AppA,然后在调试模式下安装 AppB。

在后一种情况下,我看到以下错误:

请求来自 com.micode.primaryapplication(uid 10226)的代码在进程 com.micode.secondaryapplication(uid 10227)中运行

执行以下行时会遇到此错误。

this.createPackageContext("com.micode.primaryapplication", Context.CONTEXT_INCLUDE_CODE);

这让我相信问题出在安装上,即在我看来 shareduserid 已被忽略,因此这两个应用程序没有共享相同的 UID,而是具有唯一的 UID。

需要明确的是,我只有两个使用这些共享用户 ID 的应用程序。

非常感谢任何帮助...

【问题讨论】:

    标签: android shared-data


    【解决方案1】:

    首先,我真的不推荐这种技术。 sharedUserId 主要用于预装应用程序。特别是,一旦你发布了一个应用程序,你就永远不能改变sharedUserId(包括定义一个你以前没有的)。如果您更改sharedUserId,并且用户从旧的sharedUserId 升级到新的,您的应用将无法再访问您的应用自己的文件,因为它们归旧的sharedUserId 所有。

    话虽如此,android:sharedUserId is an attribute on &lt;manifest&gt;,而不是&lt;application&gt;。

    【讨论】:

    • 嗨 CommonsWare。感谢您的建议和标记我的错误。两者都非常感谢。这是我演示解决方案所必需的,但根据上面的反馈和我读过的其他人的反馈,我当然可以建议正确的实现是使用内容提供程序。非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 2017-05-13
    • 2020-03-18
    相关资源
    最近更新 更多