2016-03-10 72 views
0

我正在將IntentService電話從MainActivity發送到名爲MapClass的類。 MapClass有一個名爲MapClassInner的內部類,它計算用戶的當前座標。但是,外部類包含onHandleIntent方法,該方法最終將使用廣播將座標發送回MainActivity。我的問題是 - 我如何訪問MapClassInner的內部變量?我很確定答案很簡單,但我一直在這個項目上連續2個小時,所以我的想法變得有點錯誤。無法從內部類檢索LatLng

下面的代碼(MapClass.java):

import android.app.IntentService; 
import android.content.Intent; 
import android.location.Location; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.location.LocationServices; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.model.LatLng; 
import org.greenrobot.eventbus.EventBus; 
import org.greenrobot.eventbus.Subscribe; 
import meetup.be2015.gcm_meetup.MainActivity.MyLocationReceiver; 


public class MapClass extends IntentService { 
    public static final String REQUEST_STRING = "myRequest"; 
    public static final String RESPONSE_LAT = "lat"; 
    public static final String RESPONSE_LNG = "lng"; 
    public GoogleMap map; 

public MapClass() { 
    super("MapClass"); 
} 

private class MapClassInner extends AppCompatActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { 

    private GoogleApiClient mGoogleApiClient; 
    private GoogleMap mgoogleMap; 
    private LatLng latLng; 

    private GoogleApiClient client; 

    public MapClassInner(GoogleMap map){ 
     this.mgoogleMap = map; 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     Log.d("ON_MAP_READY","In OnMapReady"); 
     mgoogleMap = googleMap; 
     mgoogleMap.setMyLocationEnabled(true);  //Sets location to current position 
     buildGoogleApiClient(); 
     mGoogleApiClient.connect(); 
    } 

    protected synchronized void buildGoogleApiClient() { 
     Log.d("BUILD", "In buildClient()"); 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 
     Log.d("ON_CONNECTED","In onConnected()"); 
     Location MLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
     if (MLastLocation != null) { 
      latLng = new LatLng(MLastLocation.getLatitude(), MLastLocation.getLongitude()); 


     } else { 
      Log.d("onConnected", "Value of LatLng is NULL"); 
      latLng = new LatLng(0, 0); 
     } 
    } 

    public LatLng getLatLng(){ 
     return this.latLng; 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
     //Notify 
     Log.d("ConnectionSuspended", "Connection Suspended. Status: " + i); 
     mgoogleMap.clear(); 
     mGoogleApiClient.disconnect(); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     //Notify 
     Log.d("ConnectionFailed", "Connection Failed. Status: " + connectionResult.toString()); 
     mgoogleMap.clear(); 
     mGoogleApiClient.disconnect(); 
    } 
} 



@Override 
protected void onHandleIntent(Intent intent) { 
    //Instantiating the inner class 
    MapClassInner inner = new MapClassInner(map); 

    //Trying to access the inner LatLng variable 
    LatLng coords = inner.getLatLng(); 

    //Gives me a NullPointerException 

    //Converting them to String values 
    String lat = String.valueOf(coords.latitude); 
    String lng = String.valueOf(coords.longitude); 

    //Sending broadcast Intent 
    Intent broadcastIntent = new Intent(); 
    broadcastIntent.setAction(MyLocationReceiver.PROCESS_RESPONSE); 
    broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT); 
    broadcastIntent.putExtra(RESPONSE_LAT, lat); 
    broadcastIntent.putExtra(RESPONSE_LNG, lng); 
    sendBroadcast(broadcastIntent); 
} 
} 

我怎樣才能從內部類的LatLng?我正在考慮爲此的EventBus。它會起作用嗎?是的,請對我輕鬆一點;我對Java的Android &比較陌生。

+0

轉'私人經緯度的latLng;''到經緯度的latLng;' – injecteer

回答

1

我在所有代碼中所做的簡化操作是創建一個靜態的'AppData.java'類,並且可以從我的包中的任何類訪問。然後我把這樣的數據放在那裏。

public class AppData 
{ 

    private static final AppData appData = new AppData(); // Make a static instance so it is not re-inited. 

    // My data 
    public Location lastKnownLocation; 

    /** 
    * Method to return the static instance of the AppData. 
    * 
    * @return An AppData object that is static so you can get/set global data across the application. 
    */ 
    public static AppData getInstance() {return appData;} 
} 

您可以添加getters和setter。我也給了我一個構造函數,這個構造函數使用了一個Context,並且只從我的主活動中調用過一次,它讓我可以在類中實現Preference Management,這有助於我爲保持持久性而存儲更復雜的數據。

在任何地方使用它,只是這樣做:

AppData appData = AppData.getInstance(); 
appData.lastKnownLocation = <whatever your code does> 

等...

+0

嗯,有趣。我會試試看,如果它的工作原因,你的答案被接受爲接受。謝謝! :) –