2016-07-31 144 views
0

我试图写一个简单的Android的活动,获取当前设备的位置和显示。清单和代码如下。对LocationServices.FusedLocationApi.getLastLocation()的调用始终返回null。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

这是预期。如果最后一个已知位置有一个,它将只返回一个位置。如果没有其他人使用位置API,则不会。永远不要依赖它返回一个值。相反,如果您需要位置,请注册位置更新。