You can't directly mark an marker or put route into your activity from the service...
So it is better to implement both the service and activity with a location listener...
You can track the locations in the service and store it in a variable (maybe JSON Array, String Array etc...) based on your need and store the array in the preference file...
Once after unlocking the device your activity will get restarted at that time you can get the array variable from your preference and can draw route into your activity...
You can implement like this....
Service class...
private JSONArray savedTrekLat = new JSONArray(); private JSONArray savedTrekLon = new JSONArray(); @Override public void onLocationChanged(Location location) { double lat = location.getLatitude(); double lon = location.getLongitude(); savedTrekLat.put(Double.toString(lat)); savedTrekLon.put(Double.toString(lon)); edit = mPrefer.edit(); edit.putString("SAVED_TREK_LAT", savedTrekLat.toString()); edit.putString("SAVED_TREK_LON", savedTrekLon.toString()); edit.commit(); }
Your activiy class...
private static SharedPreferences mPrefer; private String mTrekedLat; private String mTrekedLon; private JSONArray trekedlat; private JSONArray trekedlon; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.map_tile); mPrefer = getSharedPreferences("Your Preference Name", this.MODE_PRIVATE); mTrekedLat = mPrefer.getString("SAVED_TREK_LAT", null); mTrekedLon = mPrefer.getString("SAVED_TREK_LON", null); try { trekedlat = new JSONArray(mTrekedLat); trekedlon = new JSONArray(mTrekedLon); } catch (Exception e) { e.printStackTrace(); } }
After this you will get the location travelled by your mobile during sleep condition and you can place the routes into the activity using your code by extracting the values from the JSON Array...