我目前正在尝试按照本教程获取 Android 设备的最后位置 - https://developer.android.com/training/location/retrieve-current.html#play-services - 但我无法通过最后一部分。
我的代码:
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) {
}
试试这个:
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
我是一名优秀的程序员,十分优秀!