Android GPS。位置监听器不想获得修复:(

时间:2011-09-13 17:20:33

标签: android gps

以下是一段代码,用于监听GPS更新并向主要活动发送通知

然而它似乎没有起作用:))

没有错误,只调用onLocationChanged!

有趣的是:当我运行其他应用程序以获得GPS修复时,之后启动我的应用程序=>调用onLocationChanged,只有精度降低,最终修复失败。

我还尝试将此代码添加到GPS侦听器=>它起作用,视野中的卫星每秒报告一次,只有修复永远不会被指责。

到底是怎么回事? :)

公共类PhoneGPSpos {

private Handler myHandler = null;
private LocationManager lm;
private LocationListener locationListener;
private Location currentBest;
Context m_context;

public PhoneGPSpos(Context context,Handler handler){

    myHandler = handler;
    m_context = context;


    lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

 //  Bundle bundle = new Bundle();
 //  boolean xtraInjection=lm.sendExtraCommand(LocationManager.GPS_PROVIDER,"force_xtra_injection",bundle);
 //  boolean timeInjection=lm.sendExtraCommand(LocationManager.GPS_PROVIDER,"force_time_injection",bundle);

   currentBest = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    locationListener = new LocationListener() 
   { 

           public void onLocationChanged(Location location)
           { 
                Log.w("Compass", "Loc");

            if (location != null)
            {
                currentBest = location;

                Message msg = new Message();

                msg.arg1 = 5;
                myHandler.sendMessage(msg);

            }
           } 

           public void onStatusChanged(String provider, int status, Bundle 
           extras) {
            Log.d("Compass", "Stat");
           } 

           public void onProviderEnabled(String provider) {
            Log.d("Compass", "Prov e");
           } 

           public void onProviderDisabled(String provider) {
            Log.d("Compass", "Prov d");
        } 
   }; 
}

public void requestGPS()
{
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    Log.d("Compass", "gps requested");
}

public void stopGPS()
{
    Log.d("Compass", "gps stopped");
    lm.removeUpdates(locationListener);
}

public synchronized  Location getBest()
{
    return  currentBest;
}

}

3 个答案:

答案 0 :(得分:1)

好的,弄清楚这是其他人也看到过的问题:显然(至少在某些手机上)如果相机处于活动状态,GPS听众的工作情况会更糟。

Problem with getting GPS data when using CameraPreview

Android: Losing GPS signal strength on some phones when I start up the Camera and SurfaceView

Camera Preview and GPS reading in Android

http://code.google.com/p/android/issues/detail?id=20098

创建了Andorid问题

如果有人有解决方案请分享

答案 1 :(得分:0)

Check for permissions in your projects AndroidManifest.xml file

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

答案 2 :(得分:0)

你在哪里测试?在室内? 其他应用使用哪些提供商?网络提供商?

我的猜测是正在使用的应用程序使用网络提供程序,并且由于您获得了最后一次知道,它将从之前的应用程序位置ping返回该位置。网络提供商通过提供一般位置来帮助分配gps卫星。因此,当您启动应用程序(运行其他应用程序之后)后,您拥有卫星,但(因为您可能在室内)没有网络,您可能会一点一点地丢失卫星,从而降低准确性。

故事的道德:同时使用网络提供商,或确保您有清晰的天空视图来测试GPS。