我正在开发一款简单的GPS应用程序,但我在使用de&#gps'提供商。
问题在于,当我向' gps'请求更新时提供者,永远不会调用onLocationChanged。我已经授予了所需的所有权限,并且我已经测试了“gps'提供程序已启用。我已经在两部不同的手机上测试了它,但它们都没有工作(所以问题不能成为智能手机的GPS)。
此外,如果我更改提供商并使用“网络”#39;一,它使用相同的代码,也就是说,定期调用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 :(得分:0)
GPS取决于来自地球静止卫星的信号,通常存在严重问题或者根本不在室内工作。
当然,一种解决方案是户外测试。这可能并不总是最方便的选择。人们也可以从头开始编写完全模拟GPS的测试代码,但Android确实允许虚假位置提供商的实施。好处是使用真实提供商和虚假提供商的代码可以完全相同。
例如this article by PhoneArena解释了如何使用虚假的位置提供商。步骤是:
这是一个非常简短和基本的答案,以避免仅在评论中回答问题,并且可能还有更多关于GPS问题和Stack Overflow上其他位置欺骗的内容。