2011-01-25 127 views
0

我正在Android中實現需要訪問Location對象的應用程序。 我在對象中運行此對象的所有後勤。Android - 服務變量訪問

然後我綁定我的活動到該服務,ServiceGPS.java,服務和位置更新似乎工作正常,但問題是,我不能訪問ServiceGPS內的位置對象。

到服務的連接被定義爲我在Activity類

private ServiceConnection mConnection = new ServiceConnection() {  
    @Override 
    public void onServiceConnected(ComponentName className, IBinder service) { 
     @SuppressWarnings("unchecked") 
     ServiceGPS mService = ((LocalBinder<ServiceGPS>)service).getService(); 
     // Get hold of your resource, db etc and pass it through to a object that will use it 
     mLocationManager = mService.getmLocation();   
    } 

    @Override 
    public void onServiceDisconnected(ComponentName arg0) { 
     // TODO Auto-generated method stub   
    } 
}; 

如果我調試代碼,mLocationManager recibes正確的值更新一個全局變量。但我想在我的活動中訪問mService.getmLocation(),但我不能。

因爲我不能聲明mService作爲類成員。

有什麼建議嗎?

非常感謝您的無價幫助。

BR, David。

回答