I have the need to store a created ID in an android device, this ID should be created by the first of the several of ours apps that is installed in the device, and should be read by any other app that is in the device. It also needs to work with all Android devices.
I tried using content providers but it looks like I can't have several content providers with the same authority, and having a different authority for each content provider which would search all the authorities of every app isn't a viable option.
I also tried modifying the user dictionary, but some android versions, especially Samsung's, don't allow to set the dictionary or don't have installed the dictionary that comes by default, so I don't have access to that.
So my question would be:
How can I have a global variable, a string, that can be created and read by any app installed in the device? Is there any way to achieve this? Some kind of global dictionary or something like that?
Best Approach
ContentProviders I think If you are looking for best approach to follow .
Even in future for more data you should go with this option it is a good approach to share data between applications.
Alernative Approach
App 1 (for ex:app1 package name is "com.app1" ).
SharedPreferences prefs = getSharedPreferences("pref1",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("stringDemo1", strShareValue);
editor.commit();
App2(you can use it for other 30 application as ur requirement)( fetch data from Shared Preferences in 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());
}
In both app1 and app2 upto app 30 manifest files add same shared user id & label,
android:sharedUserId="any string"
android:sharedUserLabel="@string/any_string"
both are same... and shared user label must from string.xml
to understand better check below example.
<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">