将数据从服务传输到 activity
Tranfer Data from service to activity
当用户终止应用程序时,我的服务在后台 运行。
它有一个按钮,我可以 return 到地图 activity.However,当用户在销毁应用程序后通过通知按钮 return 到应用程序时,地图已创建但我正在传输的信息从我的服务到我的地图 activity 为空。
当用户没有关闭应用程序并且return通过通知按钮时,数据存在。
这是我的代码:
//Map Activity
//OnPause = transfer the data to service intent(Working fine)
BackgroundLocation backgroundLocation = new BackgroundLocation();
mServiceIntent = new Intent(this, backgroundLocation.getClass());
if (!isMyServiceRunning(backgroundLocation.getClass())) {
mServiceIntent.putExtra("AddressBackgound",mAddress);
mServiceIntent.putExtra("AddressLatBackgound",destinationLat);
mServiceIntent.putExtra("AddressLngBackgound",destinationLng);
startService(mServiceIntent);
}
// OnMapReady = Getting the data from service intent(return null for all data)
if (myLocation != null) {
BackgroundLocation backgroundLocation = new BackgroundLocation();
mServiceIntent = new Intent(this, backgroundLocation.getClass());
Bundle extras = getIntent().getExtras();
if (isMyServiceRunning(backgroundLocation.getClass()) && extras != null) {
String mAddress2 = extras.getString("AddressBackgound22");
Double destinationLat2 = extras.getDouble("AddressLatBackgound22");
Double destinationLng2 = extras.getDouble("AddressLngBackgound22");
Log.e("onResume", "onResume stats");
Log.e("Address", "" + mAddress2);
Log.e("Lat", String.valueOf(destinationLat2));
Log.e("Lng", String.valueOf(destinationLng2));
Log.e("OnMapReady","Service is running....");
}
else{
Log.e("OnMapReady","Service is not running");
}
}
后台位置(服务意图)= 从 MapsActivity 获取信息,return向 MapsActivity 获取信息。
//Service Intent
// OnStartCommand
Bundle extras = intent.getExtras();
if (extras != null) {
//getting the data to the service is working fine even when the app killed the service still working with the data.
mAddress = extras.getString("AddressBackgound");
destinationLat = extras.getDouble("AddressLatBackgound");
destinationLng = extras.getDouble("AddressLngBackgound");
//This is what I am trying to send to MapsActivity:
extras.putString("AddressBackgound22",mAddress);
extras.putDouble("AddressLatBackgound22",destinationLat);
extras.putDouble("AddressLngBackgound22",destinationLng);
Log.e("onStartCommand", "onStartCommand started");
Log.e("Address","" + mAddress);
Log.e("Lat", "" + destinationLat);
Log.e("Lng", "" + destinationLng);
}
感谢您的宝贵时间。
您可以使用事件总线或广播接收器将数据从服务发送到 Activity/Fragment
方法很多,我说其中的一些:
1-在服务中使用存储数据(例如 SharedPrefrences、DB 和...)并在 activity
中检索
2-使用事件总线或广播接收器
3-使用回调模式从服务到活动的回调数据
感谢@Mohammadreza,我在服务意图上使用了 sharedPref 并且它起作用了。
这是我所做的:
public static final String MY_PREFS_NAME = "MyData";
//保存SharedPref(BackgroundActivity):
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("AddressBackgound",mAddress);
editor.putString("AddressLatBackgound", String.valueOf(destinationLat));
editor.putString("AddressLngBackgound", String.valueOf(destinationLng));
editor.apply();
//读取SharedPref(MapsActivity):
SharedPreferences prefs = getSharedPreferences(BackgroundLocation.MY_PREFS_NAME, MODE_PRIVATE);
BackgroundLocation backgroundLocation = new BackgroundLocation();
if (isMyServiceRunning(backgroundLocation.getClass()) && prefs != null ) {
String mAddress = prefs.getString("AddressBackgound","mAddress");
Double UserStartLocationLat = Double.parseDouble(String.valueOf(Double.valueOf(prefs.getString("UserStartLocationLat", "UserStartLocationLatNull"))));
Double UserStartLocationLng = Double.parseDouble(String.valueOf(Double.valueOf(prefs.getString("UserStartLocationLng", "UserStartLocationLngNull"))));
}
isMyServiceRunning:
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
Log.i ("Service status", "Running");
return true;
}
}
Log.i ("Service status", "Not running");
return false;
}
当用户终止应用程序时,我的服务在后台 运行。 它有一个按钮,我可以 return 到地图 activity.However,当用户在销毁应用程序后通过通知按钮 return 到应用程序时,地图已创建但我正在传输的信息从我的服务到我的地图 activity 为空。
当用户没有关闭应用程序并且return通过通知按钮时,数据存在。
这是我的代码:
//Map Activity
//OnPause = transfer the data to service intent(Working fine)
BackgroundLocation backgroundLocation = new BackgroundLocation();
mServiceIntent = new Intent(this, backgroundLocation.getClass());
if (!isMyServiceRunning(backgroundLocation.getClass())) {
mServiceIntent.putExtra("AddressBackgound",mAddress);
mServiceIntent.putExtra("AddressLatBackgound",destinationLat);
mServiceIntent.putExtra("AddressLngBackgound",destinationLng);
startService(mServiceIntent);
}
// OnMapReady = Getting the data from service intent(return null for all data)
if (myLocation != null) {
BackgroundLocation backgroundLocation = new BackgroundLocation();
mServiceIntent = new Intent(this, backgroundLocation.getClass());
Bundle extras = getIntent().getExtras();
if (isMyServiceRunning(backgroundLocation.getClass()) && extras != null) {
String mAddress2 = extras.getString("AddressBackgound22");
Double destinationLat2 = extras.getDouble("AddressLatBackgound22");
Double destinationLng2 = extras.getDouble("AddressLngBackgound22");
Log.e("onResume", "onResume stats");
Log.e("Address", "" + mAddress2);
Log.e("Lat", String.valueOf(destinationLat2));
Log.e("Lng", String.valueOf(destinationLng2));
Log.e("OnMapReady","Service is running....");
}
else{
Log.e("OnMapReady","Service is not running");
}
}
后台位置(服务意图)= 从 MapsActivity 获取信息,return向 MapsActivity 获取信息。
//Service Intent
// OnStartCommand
Bundle extras = intent.getExtras();
if (extras != null) {
//getting the data to the service is working fine even when the app killed the service still working with the data.
mAddress = extras.getString("AddressBackgound");
destinationLat = extras.getDouble("AddressLatBackgound");
destinationLng = extras.getDouble("AddressLngBackgound");
//This is what I am trying to send to MapsActivity:
extras.putString("AddressBackgound22",mAddress);
extras.putDouble("AddressLatBackgound22",destinationLat);
extras.putDouble("AddressLngBackgound22",destinationLng);
Log.e("onStartCommand", "onStartCommand started");
Log.e("Address","" + mAddress);
Log.e("Lat", "" + destinationLat);
Log.e("Lng", "" + destinationLng);
}
感谢您的宝贵时间。
您可以使用事件总线或广播接收器将数据从服务发送到 Activity/Fragment
方法很多,我说其中的一些:
1-在服务中使用存储数据(例如 SharedPrefrences、DB 和...)并在 activity
中检索2-使用事件总线或广播接收器
3-使用回调模式从服务到活动的回调数据
感谢@Mohammadreza,我在服务意图上使用了 sharedPref 并且它起作用了。
这是我所做的:
public static final String MY_PREFS_NAME = "MyData";
//保存SharedPref(BackgroundActivity):
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("AddressBackgound",mAddress);
editor.putString("AddressLatBackgound", String.valueOf(destinationLat));
editor.putString("AddressLngBackgound", String.valueOf(destinationLng));
editor.apply();
//读取SharedPref(MapsActivity):
SharedPreferences prefs = getSharedPreferences(BackgroundLocation.MY_PREFS_NAME, MODE_PRIVATE);
BackgroundLocation backgroundLocation = new BackgroundLocation();
if (isMyServiceRunning(backgroundLocation.getClass()) && prefs != null ) {
String mAddress = prefs.getString("AddressBackgound","mAddress");
Double UserStartLocationLat = Double.parseDouble(String.valueOf(Double.valueOf(prefs.getString("UserStartLocationLat", "UserStartLocationLatNull"))));
Double UserStartLocationLng = Double.parseDouble(String.valueOf(Double.valueOf(prefs.getString("UserStartLocationLng", "UserStartLocationLngNull"))));
}
isMyServiceRunning:
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
Log.i ("Service status", "Running");
return true;
}
}
Log.i ("Service status", "Not running");
return false;
}