Ale*_*gel 5 android google-maps
我正在使用不同的进程(在清单中使用android:process)以能够在我的应用程序中使用多个 mapView(如何在每个Android应用程序/进程中使用多个MapActivities / MapViews)。这2张地图位于我的一般Tabhost的不同活动和不同标签中。
否,我想缩放到特定位置,该位置是用户在其他活动中选择的。使用静态变量无效。之后,我尝试将数据保存到SharedPreferences文件中,然后在MapActivity中再次读取它。但这也不起作用。数据已成功写入,但是MapActivity在SharedPreferences中找不到任何数据。
是否可以在两个或多个进程之间共享数据?
保存位置数据:
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();
}
Run Code Online (Sandbox Code Playgroud)
读取位置数据:
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);
}
Run Code Online (Sandbox Code Playgroud)
一个简单的方法是SharedPreferences。使用“ Context.MODE_MULTI_PROCESS”标志获取您的共享首选项。
在编写过程中:
SharedPreferences preferencesWriter = basedContext.getSharedPreferences("Keeps_a_constant_preferences_file_name", Context.MODE_MULTI_PROCESS);
preferencesWriter.edit().putString(someKey, savingValue).commit();
Run Code Online (Sandbox Code Playgroud)
在阅读器过程中:
SharedPreferences preferencesReader = basedContext.getSharedPreferences("Keeps_a_constant_preferences_file_name", Context.MODE_MULTI_PROCESS);
String savedValueInWriterProcess = preferencesWriter.getString(someKey, defaultValue);
Run Code Online (Sandbox Code Playgroud)
注意:在阅读器过程中,您每次都必须检索一个新的SharedPreferences变量,以确保刷新共享值。
其他方法:1.发送带有额外数据的广播;2.内容提供者。
| 归档时间: |
|
| 查看次数: |
4763 次 |
| 最近记录: |