2017-04-03 65 views
0

我正在開發一個簡單的GPS應用程序,但我在德'GPS'提供商有一些麻煩。OnLocationChanged從來沒有打電話時,GPS是供應商

問題是,當我從'gps'提供者請求更新時,onLocationChanged永遠不會被調用。我已經授予了所需的所有權限,並且已經測試了「gps」提供程序已啓用。我已經在2種不同的手機上進行了測試,並且兩種手機都無法正常工作(所以問題不能成爲我的智能手機的GPS)。此外,如果我更改提供程序並使用'網絡'之一,它可以使用相同的代碼,也就是說,定期調用onLocationChanged。

這裏是我的代碼:

public class Capturador extends Activity implements SurfaceHolder.Callback, SensorEventListener, LocationListener { 

boolean lockLocalizacion = false; 

private static final long TIEMPO_MIN = 0;//10 * 1000; // 10 segundos 
private static final long DISTANCIA_MIN = 0;//5; // 5 metros 
private LocationManager manejador; 
private String gpsLatActual; 
private String gpsLongActual; 
private String gpsAltActual; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    getWindow().setFormat(PixelFormat.TRANSPARENT); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 

    setContentView(R.layout.primeracapa); 

    manejador = (LocationManager) getSystemService(LOCATION_SERVICE); 

    Location localizacion = getLastKnownLocation(); 

    txtGPS = (TextView) findViewById(R.id.localizacionEdit); 
    if (localizacion == null) { 
     txtGPS.setText(R.string.loc_unknown); 
     gpsLatActual = ""; 
     gpsLongActual = ""; 
     gpsAltActual = ""; 
    } else { 
     txtGPS.setText("(" + Double.toString(localizacion.getLongitude()) + ", " + Double.toString(localizacion.getLatitude()) + ", " 
       + Double.toString(localizacion.getAltitude()) + ")"); 
     gpsLatActual = Double.toString(localizacion.getLatitude()); 
     gpsLongActual = Double.toString(localizacion.getLongitude()); 
     gpsAltActual = Double.toString(localizacion.getAltitude()); 
     lockLocalizacion = true; 
    } 
} 

private Location getLastKnownLocation() { 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED 
      || ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
     manejador = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE); 
     List<String> providers = manejador.getProviders(true); 
     Location bestLocation = null; 
     for (String provider : providers) { 
      Location l = manejador.getLastKnownLocation(provider); 
      if (l == null) { 
       continue; 
      } 
      if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) { 
       // Found best last known location: %s", l); 
       bestLocation = l; 
      } 
     } 
     return bestLocation; 
    } 
    else return null; 
} 

@Override 
protected void onResume() { 
    super.onResume(); 

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
     Criteria crit = new Criteria(); 
     crit.setAccuracy(Criteria.ACCURACY_FINE); 
     String best = manejador.getBestProvider(crit, true); //best is always gps (LocationManager.GPS_PROVIDER) 
     manejador.requestLocationUpdates(best, 0, 1, this); //writting LocationManager.NETWORK_PROVIDER insted of 'best' does work 
    } 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    try { 
     manejador.removeUpdates(this); 
    } catch (SecurityException e) { 
     e.printStackTrace(); 
    } 
} 

@Override 
public void onLocationChanged(Location location) { 
    if (location == null) { 
     txtGPS.setText(R.string.loc_desconocida); 
     lockLocalizacion = false; 
    } else { 
     txtGPS.setText("(" + Double.toString(location.getLatitude()) + ", " + Double.toString(location.getLongitude()) + ", " 
       + Double.toString(location.getAltitude()) + ")"); 
     gpsLatActual = Double.toString(location.getLatitude()); 
     gpsLongActual = Double.toString(location.getLongitude()); 
     gpsAltActual = Double.toString(location.getAltitude()); 
     lockLocalizacion = true; 
    } 
} 

@Override 
public void onProviderDisabled(String proveedor) { 
} 

@Override 
public void onProviderEnabled(String proveedor) { 
} 

@Override 
public void onStatusChanged(String proveedor, int estado, Bundle extras) { 


} 
} 

而且清單:

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

<uses-permission android:name="android.permission.CAMERA"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
<uses-permission android:name="android.permission.INTERNET"/> 
<uses-feature android:name="android.hardware.location.gps" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:screenOrientation="landscape" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name="Capturador" 
     android:screenOrientation="landscape"> 
    </activity> 
    <activity 
     android:name="Visualizador" 
     android:screenOrientation="landscape"> 
    </activity> 
    <activity android:name="Ajustador" android:screenOrientation="landscape"/> 
    <activity android:name="Visualizador2" android:screenOrientation="landscape"/> 
</application> 

+0

你能告訴我們你的清單的其餘部分嗎? – user1506104

+1

你在室內測試嗎? GPS在這種情況下可能會遇到問題。 –

+0

有清單。是的,我正在室內進行測試,我會嘗試在室外進行測試,這可能是問題所在。無論如何,我啓動了Google地圖,它知道我的位置,但我的應用沒有。 Google地圖是否僅使用GPS提供商或取決於可用的GPS提供商? – JOSEMAFUEN

回答

0

GPS依賴於來自地球同步衛星的信號,通常有嚴重的問題,或者在所有室內不起作用。

當然,一個解決方案是在戶外進行測試。這可能並不總是最方便的選擇。人們也可以從頭開始編寫模擬GPS的測試代碼,但Android確實允許僞造位置提供者的實現。好處是使用真正的提供者和假提供者的代碼可以完全相同。

有例如this article by PhoneArena解釋如何使用虛假的位置提供商。步驟如下:

  • 更改設備的位置設置使用GPS只
  • 從Play商店獲取假的位置提供或寫自己的
  • 的開發模式需要在設備上啓用
  • 在開發人員選項中啓用「允許模擬位置」
  • 從對話框中選擇模擬位置提供程序(適用於Android 6及更高版本)
  • 使用您用來模擬位置的任何假位置提供程序或一系列位置更新。

這是一個非常簡短的基本答案,以避免僅在評論中回答問題,並且可能還有更多要閱讀關於GPS堆棧溢出中其他地方的GPS問題和位置欺騙的信息。