如何共享两个进程之间的数据 [英] How to share data between two processes

查看:148
本文介绍了如何共享两个进程之间的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用不同的工艺(采用了android:在我的清单进程)能在我的应用程序使用多个图形页面(<一个href=\"http://stackoverflow.com/questions/3379575/how-to-use-multiple-mapactivities-mapviews-per-android-application-process\">How使用每个Android应用程序/流程多个MapActivities / MapViews)。 2.地图是在不同的活动,我一般tabhost不同的标签。

没有欲放大到特定的位置,这是通过用户在其他活动选择。使用静态变量没有工作。从那以后,我一直试图将数据保存到共享preferences文件,并在MapActivity看了一遍。但是,这也不起作用。该DATAS成功编写的,但是MapActivity没有找到在共享preferences任何数据。

有没有分享两个或多个进程之间的数据可能?

保存位置数据:

 公共静态布尔addLocationToShared(北纬浮动,浮动LON){
    Log.i(TAG,addLocationToShared:+纬度+,+ LON);
    如果(mapShared == NULL)
        。mapShared = TLApplication.getAppContext()getShared preferences(MAP_SHARED_ preFS,Context.MODE_PRIVATE);
    返回mapShared.edit()putFloat(preF_LAT,LAT).putFloat(preF_LON,LON).commit()。
}

阅读位置数据:

  mapShared = TLApplication.getAppContext()getShared preferences(MAP_SHARED_ preFS,Context.MODE_PRIVATE);
浮经纬度= mapShared.getFloat(preF_LAT,1000);
浮LON = mapShared.getFloat(preF_LON,1000);
Log.d(TAG,缩小为+纬度+,+ LON);
如果(LAT = 1000&安培;!&安培;经度= 1000!){
    的GeoPoint点=新的GeoPoint((INT)(LAT * 1E6),(INT)(LON * 1E6));
    zoomToLocation(点,15);
}


解决方案

一个简单的方法是共享preferences。使用Context.MODE_MULTI_PROCESS'标志,让您的共享preferences。

在写进程:

 共享preferences preferencesWriter = basedContext.getShared preferences(Keeps_a_constant_ preferences_file_name,Context.MODE_MULTI_PROCESS);
preferencesWriter.edit()putString(someKey,savingValue).commit()。

在阅读的过程:

 共享preferences preferencesReader = basedContext.getShared preferences(Keeps_a_constant_ preferences_file_name,Context.MODE_MULTI_PROCESS);
字符串savedValueInWriterProcess = preferencesWriter.getString(someKey,设置defaultValue);

请注意:在阅读过程中,你必须获取一个新的共享preferences变种每一次,以确保共同的价值被刷新

其他方法:
1.额外的数据发送广播;
2.内容提供商。

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);
}

解决方案

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.

这篇关于如何共享两个进程之间的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆