0

こんにちは、今日私はAndroidアプリケーションに位置情報を実装しようとしていましたが、私は理解できない異常な問題にぶつかりました。AndroidのgetLastLocationは常にnullを返します

問題

私は最後の既知の位置を取得するためのグーグルの公式文書を見ることで開始し、成功せず、それを実装しました。私は提供されたサンプルアプリケーションを見て、自分のアプリケーションに正確なコードをコピーしましたが、これも失敗しました。アプリケーションは決してエラーを生成せず、想定されているとおりに動作しますが、私は単に場所を取得していません。

下記のコードmLastLocationは、アプリケーションでは常にnullですが、私は作成していますが、作成されたGoogleでは完全に動作します。この正確なコードは、私とGoogleの両方で使用されています。

MainActivity

import android.location.Location; 
import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks; 
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener; 
import com.google.android.gms.location.LocationServices; 

public class MainActivity extends AppCompatActivity implements 
    ConnectionCallbacks, OnConnectionFailedListener { 

protected static final String TAG = "MainActivity"; 

/** 
* Provides the entry point to Google Play services. 
*/ 
protected GoogleApiClient mGoogleApiClient; 

/** 
* Represents a geographical location. 
*/ 
protected Location mLastLocation; 

protected String mLatitudeLabel; 
protected String mLongitudeLabel; 
protected TextView mLatitudeText; 
protected TextView mLongitudeText; 



@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_activity); 

    mLatitudeLabel = getResources().getString(R.string.latitude_label); 
    mLongitudeLabel = getResources().getString(R.string.longitude_label); 
    mLatitudeText = (TextView) findViewById((R.id.latitude_text)); 
    mLongitudeText = (TextView) findViewById((R.id.longitude_text)); 

    buildGoogleApiClient(); 
} 

/** 
* Builds a GoogleApiClient. Uses the addApi() method to request the LocationServices API. 
*/ 
protected synchronized void buildGoogleApiClient() { 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 
} 

@Override 
protected void onStart() { 
    super.onStart(); 
    mGoogleApiClient.connect(); 
} 

@Override 
protected void onStop() { 
    super.onStop(); 
    if (mGoogleApiClient.isConnected()) { 
     mGoogleApiClient.disconnect(); 
    } 
} 

/** 
* Runs when a GoogleApiClient object successfully connects. 
*/ 
@Override 
public void onConnected(Bundle connectionHint) { 
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
    if (mLastLocation != null) { 
     mLatitudeText.setText(String.format("%s: %f", mLatitudeLabel, 
       mLastLocation.getLatitude())); 
     mLongitudeText.setText(String.format("%s: %f", mLongitudeLabel, 
       mLastLocation.getLongitude())); 
    } else { 
     Toast.makeText(this, R.string.no_location_detected, Toast.LENGTH_LONG).show(); 
    } 
} 

@Override 
public void onConnectionFailed(ConnectionResult result) { 
    // Refer to the javadoc for ConnectionResult to see what error codes might be returned in 
    // onConnectionFailed. 
    Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode()); 
} 


@Override 
public void onConnectionSuspended(int cause) { 
    // The connection to Google Play services was lost for some reason. We call connect() to 
    // attempt to re-establish the connection. 
    Log.i(TAG, "Connection suspended"); 
    mGoogleApiClient.connect(); 
} 

}

私がこれまで行ってきたどのような他のスレッドで提案されているように

  1. メイド必ずGPSを&のWi-Fiがオンになっています。 (動作していません)
  2. 他のスレッドで提案されているように、強制的に場所を強制するGoogleマップを開始しました。 (動作していません)
  3. github repoをダウンロードし、コアファイルを新しいプロジェクトにコピーしました。

私は完全に新しいプロジェクトを最初から作成し、できるだけ公式のサンプルを複製しようとしましたが、それでも動作しませんでした。だから私はそれをダウンロードした後、直接Googleからサンプルコードを実行しようとしたともちろん、それは魅力のように動作しますが、私は理由を理解していません。 私は最後の2時間も、なぜgit repoアプリケーションが動作するのか、また、すべてのコードがまったく同じであっても、なぜ私自身の複製がないのかを理解しようとしました。

私は

で助けを必要とどのようなコードは、サンプルのレポ・アプリケーションで動作し、それはすべてのエラーを生成されていないため、コード自体に誤りはないように思えます。誰かが私に何が足りないか教えてください!

私の2つのAndroidスタジオプロジェクトでgithubレポをセットアップするのを手伝ってください。実行できないプロジェクトはBasicLocationSampleOwnと呼ばれ、動作するプロジェクトはBasicLocationSampleと呼ばれます。

あなたがここにレポをダウンロードすることができます:https://github.com/CarlOhlsson/getLastLocationProblem

答えて

1

は許可が失敗したときのAndroid 6.0は明らかに任意のエラーメッセージを生成しませんAPI 23から22にダウングレードすることで問題を解決しました。私はマニフェストファイルの権限の上にユーザからの許可を要求する必要がありました。

Android 6.0の許可情報が見つかりましたhere

関連する問題