安卓GPS。位置侦听器不想获取修复:(

发布于 2024-12-04 04:20:19 字数 2158 浏览 0 评论 0 原文

下面是一段代码,假设监听 GPS 更新并向主要活动发送通知

,但它似乎不起作用:)

没有错误,只有 onLocationChanged 没有被调用!

有趣的是:当我运行其他应用程序来修复 GPS 时,然后启动我的应用程序 => onLocationChanged 被调用,只是准确性下降,最终修复丢失。

我还尝试添加到此代码 GPS 侦听器=>它有效,每秒都会报告视野中的卫星,但从未获得修复。

到底是怎么回事? :)

public class 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;
}

}

Here is a code below that suppose to listen for GPS updates and send notifications to main activity

However it does not seem to be working :)

No errors, only onLocationChanged is not called!

Funny thing is: when I run some other app to get GPS fix and after that start my app => onLocationChanged gets called, only accuracy is degrading, and eventually fix is lost.

I also tried to add to this code GPS listener=> it works, satellites in view got reported every second, only fix is never accuired.

What the hell is going on? :)

public class 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;
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慵挽 2024-12-11 04:20:20

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

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

这个故事的寓意是:也要使用网络提供商,或者确保您有清晰的天空视野来测试 GPS。

Where are you testing? Indoors?
What providers are the other apps using? Network Provider?

My guess is that the apps that are working use Network provider and since you are getting the last known, it will return that location from the previous app location ping. The Network provider helps allocate gps satellites by giving a general location. So when you start app (after running other apps), you have satellites, but (since you are probably indoors) without the network you may lose satellites little by little, decreasing accuracy.

moral of the story: Use the network provider as well, or make sure you have clear view of the sky to test gps.

内心旳酸楚 2024-12-11 04:20:19

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

使用 CameraPreview 时获取 GPS 数据时出现问题

Android:丢失 GPS当我启动相机和 SurfaceView 时,某些手机上的信号强度

Android 中的相机预览和 GPS 读取

为此创建了 Andorid 问题 http://code.google.com/p/android/issues/detail?id=20098

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

Ok, figure out that this is an issue that was also seen by other people: apparently (at least on some phones) GPS listener works much worse if Camera is active.

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

Created Andorid issue for that http://code.google.com/p/android/issues/detail?id=20098

If someone has a solution pls share

空心空情空意 2024-12-11 04:20:19
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>
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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文