7

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);
}
Community
  • 1
  • 1
AlexVogel
  • 10,601
  • 10
  • 61
  • 71

3 Answers3

6

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.

Harry Moo
  • 77
  • 1
  • 4
  • One important note: make sure you use `.commit()` and not `.apply()` when you write to prefs to ensure that the data gets written out before you read it from another process. From the docs: "Unlike `commit()`, which writes its preferences out to persistent storage synchronously, `apply()` commits its changes to the in-memory `SharedPreferences` immediately but starts an asynchronous commit to disk and you won't be notified of any failures.... `SharedPreferences` instances are singletons **within a process** [emphasis added]." – Jarett Millard Nov 01 '14 at 19:29
  • 7
    Context.MODE_MULTI_PROCESS is considered deprecated according to docs, because it doesn't behave consistently in all cases. – mdavid Sep 16 '15 at 11:22
  • 2
    SharedPreferences doesn't support multi processes. The official docs state: Note: This class does not support use across multiple processes. – Warpzit Nov 18 '16 at 11:35
2

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.

Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
NickT
  • 23,844
  • 11
  • 78
  • 121
  • Thanks for your answer. I've looked at it, but didn't get to work with two activies. Also I think it is a litte bit overload for my purposes – AlexVogel Feb 08 '12 at 09:08
0

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.

AlexVogel
  • 10,601
  • 10
  • 61
  • 71