Reputation: 10621
I'm using different processes (using android:process in my manifest) to be able to use more than one mapView in my app (How to use multiple MapActivities/MapViews per Android application/process). The 2 maps are in different activities and different tabs of my general tabhost.
No I want to zoom to a specific location, which is selected through the user in an other activity. Using static variables didn't work. After that I've tried to save the data in to a SharedPreferences file and read it again in the MapActivity. But this also don't work. The datas are written successfully, but the MapActivity does not find any data in the SharedPreferences.
Is there a possibility to share data between two or more processes?
Saving location data:
public static boolean addLocationToShared(float lat, float lon){
Log.i(TAG, "addLocationToShared: " + lat + "," + lon);
if(mapShared==null)
mapShared = TLApplication.getAppContext().getSharedPreferences(MAP_SHARED_PREFS, Context.MODE_PRIVATE);
return mapShared.edit().putFloat(PREF_LAT, lat).putFloat(PREF_LON, lon).commit();
}
Reading location data:
mapShared = TLApplication.getAppContext().getSharedPreferences(MAP_SHARED_PREFS, Context.MODE_PRIVATE);
float lat = mapShared.getFloat(PREF_LAT, 1000);
float lon = mapShared.getFloat(PREF_LON, 1000);
Log.d(TAG, "zoom to " + lat + ", " + lon);
if(lat != 1000 && lon != 1000){
GeoPoint point = new GeoPoint((int)(lat * 1E6), (int)(lon * 1E6));
zoomToLocation(point, 15);
}
Upvotes: 7
Views: 7044
Reputation: 23873
I think Android Interface Definition Language (AIDL) may be your answer here. I've used it to communicate between an application and a remote service running from a separate apk. Marshalling the data shouldn't be that complicated if you are just passing a couple of floats.
Upvotes: 2
Reputation: 77
A simple method is the SharedPreferences. Use the 'Context.MODE_MULTI_PROCESS' flag to get your shared preferences.
In writer process:
SharedPreferences preferencesWriter = basedContext.getSharedPreferences("Keeps_a_constant_preferences_file_name", Context.MODE_MULTI_PROCESS);
preferencesWriter.edit().putString(someKey, savingValue).commit();
In reader process:
SharedPreferences preferencesReader = basedContext.getSharedPreferences("Keeps_a_constant_preferences_file_name", Context.MODE_MULTI_PROCESS);
String savedValueInWriterProcess = preferencesWriter.getString(someKey, defaultValue);
NOTE: In the reader process, you must retrieve a fresh SharedPreferences variant every time to ensure the shared value is refreshed.
Other methods: 1. Send broadcast with extra data; 2. The Content Provider.
Upvotes: 6
Reputation: 10621
I've tried JPriest's and NickT's answer, but they either didn't work or are to much overload. I ended saving the data into a file and reading it again in the other process. Not a very nice solution but at least it worked.
Upvotes: 0