2016-07-31 5 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を使用しているものがなければ、それは行いません。値を返すことに決して頼ってはいけません。場所が必要な場合は、場所の更新に登録してください。

関連する問題