Issue
When user kill app,my service is running in the background. It has a button which I can return to the map activity.However,when the user return to the app through the notification button after destroying the app,the map created but the information I am transfering from my service to my map activity is null.
When the user isn't killing the app and return through the notification button,the data exist.
This is my code:
//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");
}
}
Background Location(Service Intent) = Getting information from MapsActivity and returnthe information to MapsActivity aswell.
//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);
}
Thank you for your time.
Solution
There is a many way,I say some of that:
1-Use store data in service (such as SharedPrefrences,DB and ...) and retrive in activity
2-Use eventbus or broadcast receivers
3-Use callback patern for callback data from service to activiy
Answered By - Mohammadreza
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.