【发布时间】:2016-07-30 11:16:28
【问题描述】:
我正在使用 google play 服务来获取我最后的位置。该项目在没有强制关闭的情况下运行良好,但是,它永远不会得到任何位置。我认为它不需要 GPS 或移动网络,因为 google play 服务已经基于这两种策略获得了我的最后一个位置。我的代码应该在文本字段中设置我的位置。但它没有
public class MainActivity extends Activity implements
ConnectionCallbacks, OnConnectionFailedListener {
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
TextView tvLatlong;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvLatlong = (TextView) findViewById(R.id.textView1);
buildGoogleApiClient();
if(mGoogleApiClient!= null){
mGoogleApiClient.connect();
}
else
Toast.makeText(this, "Not connected...", Toast.LENGTH_SHORT).show();
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
Toast.makeText(this, "Failed to connect...", Toast.LENGTH_SHORT).show();
}
@Override
public void onConnected(Bundle arg0) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
tvLatlong.setText("Latitude: "+ String.valueOf(mLastLocation.getLatitude())+"Longitude: "+
String.valueOf(mLastLocation.getLongitude()));
}
}
@Override
public void onConnectionSuspended(int arg0) {
Toast.makeText(this, "Connection suspended...", Toast.LENGTH_SHORT).show();
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
【问题讨论】:
-
您是否在 Onconnected 中收到回调?
标签: android