2017-02-22 20 views
1

我目前正試圖沿着本教程獲取Android設備的最後位置 - https://developer.android.com/training/location/retrieve-current.html#play-services - 但我無法越過最後一部分。如何解決mLastLocation,mLatitudeText,mLongitudeText在獲取上一個位置

我的代碼:

package com.example.prouser.driveapp; 


public class MainActivity extends AppCompatActivity implements ConnectionCallbacks { 


/** 
* ATTENTION: This was auto-generated to implement the App Indexing API. 
* See https://g.co/AppIndexing/AndroidStudio for more information. 
*/ 
private GoogleApiClient client; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    super.onStart(); 

protected void onStart() { 
    mGoogleApiClient.connect(); 
    super.onStart(); 
} 

protected void onStop() { 
    mGoogleApiClient.disconnect(); 
    super.onStop(); 
} 

public void onConnected(Bundle connectionHint) { 

    if (mLastLocation != null) { 
     mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude())); 
     mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude())); 
    } 
} 


// Create an instance of GoogleAPIClient. 
GoogleApiClient mGoogleApiClient = new GoogleApiClient() { 
    @Override 
    public boolean hasConnectedApi(@NonNull Api<?> api) { 
     return false; 
    } 

    @NonNull 
    @Override 
    public ConnectionResult getConnectionResult(@NonNull Api<?> api) { 
     return null; 
    } 

    @Override 
    public void connect() { 

    } 

    @Override 
    public ConnectionResult blockingConnect() { 
     return null; 
    } 

    @Override 
    public ConnectionResult blockingConnect(long l, @NonNull TimeUnit timeUnit) { 
     return null; 
    } 

    @Override 
    public void disconnect() { 

    } 

    @Override 
    public void reconnect() { 

    } 

    @Override 
    public PendingResult<Status> clearDefaultAccountAndReconnect() { 
     return null; 
    } 

    @Override 
    public void stopAutoManage(@NonNull FragmentActivity fragmentActivity) { 

    } 

    @Override 
    public boolean isConnected() { 
     return false; 
    } 

    @Override 
    public boolean isConnecting() { 
     return false; 
    } 

    @Override 
    public void registerConnectionCallbacks(@NonNull ConnectionCallbacks connectionCallbacks) { 

    } 

    @Override 
    public boolean isConnectionCallbacksRegistered(@NonNull ConnectionCallbacks connectionCallbacks) { 
     return false; 
    } 

    @Override 
    public void unregisterConnectionCallbacks(@NonNull ConnectionCallbacks connectionCallbacks) { 

    } 

    @Override 
    public void registerConnectionFailedListener(@NonNull OnConnectionFailedListener onConnectionFailedListener) { 

    } 

    @Override 
    public boolean isConnectionFailedListenerRegistered(@NonNull OnConnectionFailedListener onConnectionFailedListener) { 
     return false; 
    } 

    @Override 
    public void unregisterConnectionFailedListener(@NonNull OnConnectionFailedListener onConnectionFailedListener) { 

    } 

    @Override 
    public void dump(String s, FileDescriptor fileDescriptor, PrintWriter printWriter, String[] strings) { 

    } 
}; 
if(mGoogleApiClient==null) 

{ 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener((OnConnectionFailedListener) this) 
      .addApi(LocationServices.API) 
      .build(); 
} 
} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 

} 

@Override 
public void onConnectionSuspended(int i) { 


} 

回答

0

試試這個:

public class MainActivity extends AppCompatActivity implements 
     ConnectionCallbacks, OnConnectionFailedListener { 

    protected static final String TAG = "MainActivity"; 

    /** 
    * Provides the entry point to Google Play services. 
    */ 
    protected GoogleApiClient mGoogleApiClient; 

    /** 
    * Represents a geographical location. 
    */ 
    protected Location mLastLocation; 

    protected String mLatitudeLabel; 
    protected String mLongitudeLabel; 
    protected TextView mLatitudeText; 
    protected TextView mLongitudeText; 



    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_activity); 

     mLatitudeLabel = getResources().getString(R.string.latitude_label); 
     mLongitudeLabel = getResources().getString(R.string.longitude_label); 
     mLatitudeText = (TextView) findViewById((R.id.latitude_text)); 
     mLongitudeText = (TextView) findViewById((R.id.longitude_text)); 

     buildGoogleApiClient(); 
    } 

    /** 
    * Builds a GoogleApiClient. Uses the addApi() method to request the LocationServices API. 
    */ 
    protected synchronized void buildGoogleApiClient() { 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     if (mGoogleApiClient.isConnected()) { 
      mGoogleApiClient.disconnect(); 
     } 
    } 

    /** 
    * Runs when a GoogleApiClient object successfully connects. 
    */ 
    @Override 
    public void onConnected(Bundle connectionHint) { 
     // Provides a simple way of getting a device's location and is well suited for 
     // applications that do not require a fine-grained location and that do not need location 
     // updates. Gets the best and most recent location currently available, which may be null 
     // in rare cases when a location is not available. 
     mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
     if (mLastLocation != null) { 
      mLatitudeText.setText(String.format("%s: %f", mLatitudeLabel, 
        mLastLocation.getLatitude())); 
      mLongitudeText.setText(String.format("%s: %f", mLongitudeLabel, 
        mLastLocation.getLongitude())); 
     } else { 
      Toast.makeText(this, R.string.no_location_detected, Toast.LENGTH_LONG).show(); 
     } 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult result) { 
     // Refer to the javadoc for ConnectionResult to see what error codes might be returned in 
     // onConnectionFailed. 
     Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode()); 
    } 


    @Override 
    public void onConnectionSuspended(int cause) { 
     // The connection to Google Play services was lost for some reason. We call connect() to 
     // attempt to re-establish the connection. 
     Log.i(TAG, "Connection suspended"); 
     mGoogleApiClient.connect(); 
    } 
} 

您在這裏有一個完整的例子:

https://github.com/googlesamples/android-play-location

相關問題