Android:在Service方法中操作变量的值并从不同的Activity中获取

时间:2012-03-31 14:12:25

标签: android variables binding service android-activity

我为收听用户位置实施了服务:

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

}

Location userLocation课程中有一个变量LocalBinder。 我在Log.d函数中放了onLocationChanged,所以我看到userLocation值没问题。

在第一个Activity中,我将其与我的服务绑定并调用StartListenLocation方法:

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

在第二个Activity中,我还使用Service进行绑定并调用getUserLocation()方法:

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

但是,这里userLocation变量为null,尽管来自第一个Activity的方法调用的输出中的值不为空。

我需要从我的第一个活动开始服务,并从现在起和所有其他活动期间开始更新userLocation

在下一个Activity中,我试图获得userLocation,但我得到了null。为什么会这样?

为什么我不能操作Service的方法中的变量并将其放到我需要的地方?

1 个答案:

答案 0 :(得分:1)

要获得所需的结果,Location userLocation声明应移至ListenLocationService类。

要将新值设置为userLocation,请使用以下代码:userLocation.set(location);

因为userLocation = new Location(location);导致删除最终修饰符错误,即使没有最终修饰符也是如此。