Android: manipulate a variable's value inside

2019-09-09 09:57发布

I implemented Service for listening user location:

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;
}   

}

There's a variable Location userLocation in LocalBinder class. I putted Log.d in onLocationChanged function, so I saw that userLocation value is ok.

In first Activity I bind it with my Service and call StartListenLocation method:

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);
...
}

In the second Activity I also make a bind with Service and call getUserLocation() method:

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);
...
}

However, here userLocation variable is null in spite of not null value in output from method call from first Activity.

I need to start Service from my first Activity and begin updating userLocation from now and during all other Activities work.

In next Activity I'm trying to get userLocation but I'm getting null. Why it happens?

Why I can't manipulate a variable inside Service's methods and get it where I need?

1条回答
神经病院院长
2楼-- · 2019-09-09 10:52

To achieve required result, Location userLocation declaration should be moved to ListenLocationService class.

To set new value to userLocation, use this code: userLocation.set(location);

because userLocation = new Location(location); cause remove final modifier error even if there's no final modifier.

查看更多
登录 后发表回答