ANDROID - 如何保存字符串并从任何应用程序访问它

ANDROID - How can I save a string and access it from any App

我需要在 android 设备中存储一个创建的 ID,这个 ID 应该由安装在设备中的我们的几个应用程序中的第一个创建,并且应该被任何人读取设备中的其他应用程序。它还需要与所有 Android 设备一起使用。

我试过使用内容提供者,但看起来我不能让多个内容提供者拥有相同的权限,并且为每个内容提供者设置不同的权限来搜索每个应用程序的所有权限是不可行的选项。

我也试过修改用户词典,但是有些android版本,尤其是三星的,不允许设置词典或者没有安装默认自带的词典,所以我没有我无法访问它。

所以我的问题是:

我怎样才能拥有一个全局变量,一个字符串,可以被安装在设备上的任何应用程序创建和读取?有什么办法可以做到这一点?某种全球词典或类似的东西?

最佳方法

ContentProviders 我想如果你正在寻找最好的方法。

即使将来要获取更多数据,您也应该使用此选项,这是在应用程序之间共享数据的好方法。

替代方法

App 1(ex:app1 包名称是 "com.app1")。

SharedPreferences prefs = getSharedPreferences("pref1",
                    Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("stringDemo1", strShareValue);
            editor.commit();

App2(您可以根据需要将其用于其他 30 个应用程序)(从 App 1 中的“共享首选项”获取数据)。

    try {
            con = createPackageContext("com.app1", 0);//first app package name is "com.sharedpref1"
            SharedPreferences pref = con.getSharedPreferences(
                        "pref1", Context.MODE_PRIVATE);
            String your_data = pref.getString("stringDemo1", "No Value");
        } 
    catch (NameNotFoundException e) {
                Log.e("Not data shared between two applications", e.toString());
         }

在 app1 和 app2 最多 30 个清单文件中添加相同的共享用户 ID 和标签,

 android:sharedUserId="any string" 
 android:sharedUserLabel="@string/any_string"

两者相同...并且共享用户标签必须来自 string.xml

为了更好地理解检查下面的例子。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xxxx"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="any string" 
android:sharedUserLabel="@string/any_string">

我设法找到了一种不同的方法,通过从 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();
    }