【问题标题】:LocationServices.FusedLocationApi.getLastLocation always returns nullLocationServices.FusedLocationApi.getLastLocation 始终返回 null
【发布时间】:2016-07-31 01:08:46
【问题描述】:

我正在尝试编写一个简单的 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();
    }

}

【问题讨论】:

    标签: android location-services


    【解决方案1】:

    这是意料之中的。 Last known location 将仅返回一个位置(如果它有一个位置)。如果没有其他东西在使用位置 API,它就不会。永远不要依赖它返回一个值。如果您需要位置,请注册以获取位置更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-31
      • 2015-08-15
      • 2012-03-18
      • 2016-11-04
      • 2016-07-28
      • 2010-11-08
      • 2017-11-26
      • 2015-02-23
      相关资源
      最近更新 更多