title: Android: manipulate a variable's value inside Service methods and get from different Activities
body: <p>I implemented Service for listening user location:</p>
  
  <pre><code>public class ListenLocationService extends Service {
      private final IBinder mBinder = new LocalBinder();
  
  public interface ILocationService {      
        public void StartListenLocation();
        public Location getUserLocation();
      }
  
  public class LocalBinder extends Binder implements ILocationService{
      LocationManager locationManager;
      LocationListener locationListener;
      Location userLocation = null;
  
      public void StartListenLocation()
      {   
          locationManager = (LocationManager)ListenLocationService.this.getSystemService(Context.LOCATION_SERVICE);
          locationListener = new LocationListener() {
  
              public void onStatusChanged(String provider, int status, Bundle extras) {
  
              }       
  
              public void onProviderEnabled(String provider) {
  
              }
  
              public void onProviderDisabled(String provider) {
  
              }
  
              public void onLocationChanged(Location location) {                      
                  userLocation = location;    
                  Log.d("Service", "Location changed at: "+userLocation.toString());
              }
          };
          locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 
                  0, 0, locationListener);
          locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
                  0, 0, locationListener);        
      }
  
      public Location getUserLocation(){
          Log.d("Service", "return location: "+userLocation.toString());
          return userLocation;
      }
  
      public void onPause() {
          //super.onPause();
          locationManager.removeUpdates(locationListener);
      }
  
      public void onResume() {
          //super.onResume();
          locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 
                  0, 0, locationListener);
          locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
                  0, 0, locationListener);
      }
  }
  
  @Override
  public IBinder onBind(Intent intent) {
      return mBinder;
  }   
  </code></pre>
  
  <p>}</p>
  
  <p>There's a variable <code>Location userLocation</code> in <code>LocalBinder</code> class.
  I putted <code>Log.d</code> in <code>onLocationChanged</code> function, so I saw that userLocation value is ok.</p>
  
  <p>In first Activity I bind it with my Service and call <code>StartListenLocation</code> method:</p>
  
  <pre><code>private ServiceConnection mConnection = new ServiceConnection() {
  
          public void onServiceConnected(ComponentName className,
                  IBinder service) {
              mService = (ILocationService) service; 
              mService.StartListenLocation();    
          }
  
          public void onServiceDisconnected(ComponentName arg0) {
          }
      };
  
  public void onCreate(Bundle savedInstanceState) {
  ...
          Intent intent = new Intent(this, ListenLocationService.class);
          bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
  ...
  }
  </code></pre>
  
  <p>In the second Activity I also make a bind with Service and call <code>getUserLocation()</code> method:</p>
  
  <pre><code>private ServiceConnection mConnection = new ServiceConnection() {
  
          public void onServiceConnected(ComponentName className,
                  IBinder service) {
              mService = (ILocationService) service; 
              Location userLocation = mService.getUserLocation();
              showUserLocation(userLocation);  
          }
  
          public void onServiceDisconnected(ComponentName arg0) {
          }
      };
  public void onCreate(Bundle savedInstanceState) {
  ...
  Intent intent = new Intent(this, ListenLocationService.class);
          bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
  ...
  }
  </code></pre>
  
  <p>However, here <code>userLocation</code> variable is null in spite of not null value in output from method call from first Activity.</p>
  
  <p>I need to start Service from my first Activity and begin updating <code>userLocation</code> from now and during all other Activities work.</p>
  
  <p>In next Activity I'm trying to get <code>userLocation</code> but I'm getting null. Why it happens?</p>
  
  <p>Why I can't manipulate a variable inside Service's methods and get it where I need?</p>