我正在尝试编写一个简单的Android活动来获取当前设备位置并显示它。清单和代码如下。对LocationServices.FusedLocationApi.getLastLocation()的调用始终返回null。
我已经在多台设备上尝试了这项功能,验证了位置服务已启用,确保地图正在运行等等。此应用程序并不多,所以我希望有人可以指出我的错误。
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mistersquawk.helloagain">
st
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MyActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>
</manifest>
代码:
package com.mistersquawk.helloagain;
import android.location.Location;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
public class DisplayMessageActivity extends AppCompatActivity
implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
static private GoogleApiClient mGoogleApiClient = null;
private TextView textView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
//
// Set up to display a string
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (textView == null) {
textView = new TextView(this);
}
textView.setTextSize(40);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.content);
layout.addView(textView);
//
// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle connectionHint) {
// We are now connected!
Location mLastLocation;
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if(mLastLocation != null)
textView.setText(mLastLocation.toString());
else
textView.setText("Null mLastLocation");
}
@Override
public void onConnectionSuspended(int cause) {
// We are not connected anymore!
}
public void onConnectionFailed(ConnectionResult result) {
// We tried to connect but failed!
}
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
}
答案 0 :(得分:0)
这是预期的。最后一个已知位置只会返回一个位置(如果有)。如果没有其他人使用位置API,它就不会。永远不要依赖它返回一个值。而是在需要位置时注册位置更新。