Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share data between two processes

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);
}
like image 279
AlexVogel Avatar asked Feb 07 '12 15:02

AlexVogel


People also ask

How to share data between 2 processes?

One common way is to use files to communicate between the processed. Each can write to a specific file that the other reads from. You can use WCF, the registry, network interface, message queues or any other mechanism that lives outside the process. The registry as an IPC mechanism.

Can two processes share memory?

Context switching between processes is more expensive. Processes don't share memory with other processes. Threads share memory with other threads of the same process.


2 Answers

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.

like image 138
Harry Moo Avatar answered Oct 18 '22 01:10

Harry Moo


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.

like image 24
NickT Avatar answered Oct 18 '22 01:10

NickT