2016-07-31 8 views
0

현재 Android 위치를 가져 와서 표시하는 간단한 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를 사용하지 않는다면 그렇지 않습니다. 값을 반환하는 것에 의존하지 마십시오. 위치가 필요한 경우 위치 업데이트를 등록하십시오.

관련 문제