让多个应用程序管理一个内容提供商 - INSTALL_FAILED_CONFLICTING_PROVIDER

Have several apps managing one content provider - INSTALL_FAILED_CONFLICTING_PROVIDER

我需要让多个应用程序使用同一个内容提供商。用户安装的第一个应用程序创建提供者并添加一个 UUID,每个其他应用程序在安装时检查该提供者是否已经存在并使用该 UUID,或者,如果之前没有安装其他应用程序,他们创建内容提供者供其他应用使用的 UUID。

我怎样才能做到这一点,让多个应用程序管理同一个内容提供者而不会出现以下错误,因为具有相同的权限而产生问题。

INSTALL_FAILED_CONFLICTING_PROVIDER

我能否以某种方式更改提供者权限并让它访问同一内容提供者?如果我更改权限并使用相同的 url,它会告诉我它无效。

谢谢!

这可能不是最好的方法。提供商 ID 在系统范围内是唯一的,您在给定时间确实不能拥有多个。但是如果你想坚持下去,你可以阅读更多关于它的信息here and here

您需要它来访问应用程序中的数据吗?最好使用 Intents 或其他一些策略作为文件或在线数据库来做到这一点。

您可以查看 Realm 以帮助解决您的问题。

我设法找到了一种不同的方法,通过从 this post 创建一个唯一标识符并使​​用 Android 相同的 ID,而 phone 则不同恢复出厂设置后,我可以拥有一个唯一的、不可更改的 ID,因此任何应用程序只需加载此 ID。

这是我使用的代码:

/**
     * Return pseudo unique ID
     * @return ID
     */
    public static String getUniquePsuedoID(Context context) {
        // If all else fails, if the user does have lower than API 9 (lower
        // than Gingerbread), has reset their device or 'Secure.ANDROID_ID'
        // returns 'null', then simply the ID returned will be solely based
        // off their Android device information. This is where the collisions
        // can happen.
        // Thanks http://www.pocketmagic.net/?p=1662!
        // Try not to use DISPLAY, HOST or ID - these items could change.
        // If there are collisions, there will be overlapping data
        String android_id = Settings.Secure.getString(context.getContentResolver(),
                Settings.Secure.ANDROID_ID);
        String m_szDevIDShort = "35" + (Build.BOARD.length() % 10) + android_id + (Build.BRAND.length() % 10) + (Build.CPU_ABI.length() % 10) + (Build.DEVICE.length() % 10) + (Build.MANUFACTURER.length() % 10) + (Build.MODEL.length() % 10) + (Build.PRODUCT.length() % 10);

        // Thanks to @Roman SL!
        // 
        // Only devices with API >= 9 have android.os.Build.SERIAL
        // http://developer.android.com/reference/android/os/Build.html#SERIAL
        // If a user upgrades software or roots their device, there will be a duplicate entry
        String serial = null;
        try {
            serial = android.os.Build.class.getField("SERIAL").get(null).toString();

            // Go ahead and return the serial for api => 9
            return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
        } catch (Exception exception) {
            // String needs to be initialized
            serial = "serial"; // some value
        }

        // Thanks @Joe!
        // 
        // Finally, combine the values we have found by using the UUID class to create a unique identifier
        return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
    }