我无法在我的 Android Wear 模拟器上获取位置信息。我已经尝试了很多教程,但没有奏效,我不明白出了什么问题。下面是我的示例源代码,它与谷歌的原始 Android Demo 非常相似。onLocationChanged 永远不会被执行,当我尝试检索最后一个位置时,我总是得到 NULL
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.support.wearable.view.WatchViewStub;
import android.view.View;
import android.widget.TextView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.wearable.Wearable;
public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {
private TextView mTextView;
private static final long UPDATE_INTERVAL_MS = 2000;
private static final long FASTEST_INTERVAL_MS = 1000;
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
mTextView = (TextView) stub.findViewById(R.id.text);
}
});
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
System.out.println("Connect to Playservices...");
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
System.out.println("onConnected");
LocationRequest locationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(UPDATE_INTERVAL_MS)
.setFastestInterval(FASTEST_INTERVAL_MS);
LocationServices.FusedLocationApi
.requestLocationUpdates(mGoogleApiClient, locationRequest, this)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (status.getStatus().isSuccess()) {
System.out.println("Successfully requested location updates"+status.getStatusMessage());
} else {
System.out.println(
"Failed in requesting location updates, "
+ "status code: "
+ status.getStatusCode() + ", message: " + status
.getStatusMessage());
}
}
});
}
public void getLocation(View view)
{
Location loc=LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if(loc!=null) {
System.out.println("Location: " + loc.getAltitude() + "\n" + loc.getProvider() + "\n" + loc.getLongitude());
}
else
{
System.out.println("Location null");
}
}
@Override
public void onConnectionSuspended(int i) {
System.out.println("onConnectionSuspended(): connection to location client suspended");
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
System.out.println("onConnectionFailed(): connection to location client failed");
}
@Override
public void onLocationChanged (Location location)
{
System.out.println("onLocationchanged: new location"+location.getAltitude());
}
@Override
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
mGoogleApiClient.disconnect();
System.out.println("onstop");
}
@Override
protected void onResume() {
super.onResume();
mGoogleApiClient.connect();
System.out.println("onresume");
}
@Override
protected void onPause() {
super.onPause();
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi
.removeLocationUpdates(mGoogleApiClient, this);
}
mGoogleApiClient.disconnect();
System.out.println("onPause");
}
}