2017-08-26 49 views
1

我想寫一個應用程序獲取當前位置並實時顯示經度和緯度。但是,當我運行這個應用程序時,GPS似乎並沒有搜索到一個位置,並嘗試獲得修復。我不知道爲什麼會發生這種情況。我錯過了什麼嗎?如果我打開地圖應用程序,位置立即得到修復。但在我的應用程序中,gps並未在通知欄中搜索一個或至少一個,它沒有顯示像通常由地圖應用程序顯示的「搜索gps」的任何內容。熔合位置不工作Android

我的主要活動:

package com.example.android.location1; 

import android.Manifest; 
import android.content.pm.PackageManager; 
import android.location.Location; 
import android.os.Build; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 

import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.location.LocationListener; 
import com.google.android.gms.location.LocationRequest; 
import com.google.android.gms.location.LocationServices; 

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, LocationListener{ 

private final String LOG_TAG = "TestApp"; 
private TextView txtOutput; 
private GoogleApiClient mGoogleApiClient; 
private LocationRequest mLocationRequest; 
private static final int REQUEST_PERMISSION = 1; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(LocationServices.API).addConnectionCallbacks(this).build(); 
    txtOutput = (TextView) findViewById(R.id.txtOutput); 
} 

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

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

@Override 
public void onConnected(Bundle bundle){ 
    mLocationRequest = LocationRequest.create(); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    mLocationRequest.setInterval(1000).setMaxWaitTime(2000); 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
      requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_PERMISSION); 
    } 
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
} 

@Override 
public void onConnectionSuspended(int i){ 
    Log.i(LOG_TAG, "GoogleApiClient connection has been suspended."); 
} 

@Override 
public void onLocationChanged(Location location){ 
    Log.i(LOG_TAG, location.toString()); 
    txtOutput.setText(String.valueOf(location.getLatitude()) + ", " + String.valueOf(location.getLongitude())); 
} 

}

我的清單文件:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.android.location1"> 

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 

    <uses-feature android:name="android.hardware.location.gps"/> 
    <uses-feature android:name="android.hardware.location.network"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version"/> 

     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

我已經測試這在Android 6和7的任何一方不工作。任何幫助,將不勝感激。

回答

-1

獲取對系統的位置管理的參考

LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

然後註冊locationupdates並將其連接到你已經有多久等implementet

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3, 1, locationListener); 

閱讀有關requestlocationsUpdates方法監聽器你的位置在哪裏。 https://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates

+0

否您的解決方案尚未解決問題。問題依然存在。 – speedster01

+0

請按照下列步驟 https://developers.google.com/android/guides/setup 檢查您的mGoogleApiClient通過認證,並連接在監控日誌,而且onConnected()方法是在你的回調GoogleApiClient.ConnectionCallbacks運行 – typecode

+0

我使用了您提供的鏈接僅用於初始設置功能。我檢查了; google客戶端連接並且onConnected()方法也運行。 locationChanged()方法也被調用。只是它是一次又一次的相同的位置。除了未在通知菜單中顯示gps圖標之外,該位置似乎沒有變化。 – speedster01