Reputation: 469
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?
Upvotes: 1
Views: 371
Reputation: 469
I have managed to get a different approach to this, by creating an Unique identifier from this post and using the Android Id that will be the same while the phone isn't factory reseted, I can have an unique, not changeable ID, so any app just load this ID.
This is the code I used:
/**
* 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!
// https://stackoverflow.com/a/4789483/950427
// 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!
// https://stackoverflow.com/a/2853253/950427
// 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();
}
Upvotes: 0
Reputation: 4353
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">
Upvotes: 1