在 Android 中获取 GPS 强度

发布于 2024-11-10 08:52:04 字数 1687 浏览 0 评论 0 原文

在我的应用程序中,我必须找出 GPS 强度。但是,当我使用以下代码时,我仅得到 0 卫星数。所以,我无法获得 GPS 强度。

我的代码:

public void onCreate(Bundle savedInstanceState) {
  locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
  locMgr.addGpsStatusListener(onGpsStatusChange) ;
  onGpsStatusChange.onGpsStatusChanged(GpsStatus.GPS_EVENT_SATELLITE_STATUS);                     
 // Listener for GPS Status...
}

            final Listener onGpsStatusChange=new GpsStatus.Listener()
            {       
                    public void onGpsStatusChanged(int event)
                    {
                        Log.e("gps","in class");
                            switch(event)
                            {
                             case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                                  GpsStatus xGpsStatus =locMgr.getGpsStatus(null) ;

                                  Iterable<GpsSatellite> iSatellites               =xGpsStatus.getSatellites();
                                  Iterator<GpsSatellite> it = iSatellites.iterator();
                                   int count=0;
                                   while(it.hasNext()){
                                    count=count+1;
                                        GpsSatellite oSat = (GpsSatellite) it.next();
                                        

                                        Log.e("gps",""+oSat.getSnr());   
                                   }
                                 Log.e("count",""+count);
                                 tv1.setText(""+count);
                             break;
                            }
                    }
            };

In my app, I have to find out GPS strength. However, when I use the following code, I get 0 only for count no of satellites. So, I could not get GPS strength.

My code:

public void onCreate(Bundle savedInstanceState) {
  locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
  locMgr.addGpsStatusListener(onGpsStatusChange) ;
  onGpsStatusChange.onGpsStatusChanged(GpsStatus.GPS_EVENT_SATELLITE_STATUS);                     
 // Listener for GPS Status...
}

            final Listener onGpsStatusChange=new GpsStatus.Listener()
            {       
                    public void onGpsStatusChanged(int event)
                    {
                        Log.e("gps","in class");
                            switch(event)
                            {
                             case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                                  GpsStatus xGpsStatus =locMgr.getGpsStatus(null) ;

                                  Iterable<GpsSatellite> iSatellites               =xGpsStatus.getSatellites();
                                  Iterator<GpsSatellite> it = iSatellites.iterator();
                                   int count=0;
                                   while(it.hasNext()){
                                    count=count+1;
                                        GpsSatellite oSat = (GpsSatellite) it.next();
                                        

                                        Log.e("gps",""+oSat.getSnr());   
                                   }
                                 Log.e("count",""+count);
                                 tv1.setText(""+count);
                             break;
                            }
                    }
            };

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

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

发布评论

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

评论(3

我不吻晚风 2024-11-17 08:52:04

您可以从 NMEA 句子中获取每个卫星的 GPS 信噪比

 $GPGSV,2,1,08,01,40,083,46,02,17,308,41,12,07,344,39,14,22,228,45*75

其中:

      GSV          Satellites in view
      2            Number of sentences for full data
      1            sentence 1 of 2
      08           Number of satellites in view

      01           Satellite PRN number
      40           Elevation, degrees
      083          Azimuth, degrees
      46           SNR - higher is better
           for up to 4 satellites per sentence
      *75          the checksum data, always begins with *

要收听 NMEA 数据,您需要创建一个 NMEA 监听器

You can get GPS signal to noise ratio of each sat from the NMEA sentances

 $GPGSV,2,1,08,01,40,083,46,02,17,308,41,12,07,344,39,14,22,228,45*75

Where:

      GSV          Satellites in view
      2            Number of sentences for full data
      1            sentence 1 of 2
      08           Number of satellites in view

      01           Satellite PRN number
      40           Elevation, degrees
      083          Azimuth, degrees
      46           SNR - higher is better
           for up to 4 satellites per sentence
      *75          the checksum data, always begins with *

To listen to NMEA data you need to create a NMEA listener.

原来分手还会想你 2024-11-17 08:52:04

我发现获取(和过滤)准确性和修复位置的最佳方法是使用以下位置找到的解决方案:
http://developer.android.com/guide/ topic/location/obtaining-user-location.html#BestEstimate ,这是迄今为止我得到的最好结果。

假设您已经有了一个好的位置并且正在使用它(例如 animateTo ..)
然后假设您将该位置保存在 Location 变量中,并将其命名为 currentBestLocation

您应该做的是将 isBetterLocation() (来自“维护当前最佳估计”)
我刚刚在您的活动中发布了 Google 网站向上)方法,然后在您的 onLocationChanged() 中发布
您应该将 currentBestLocation 和刚刚从 onLocationChanged() 获取的位置发送到 isBetterLocation()
(例如,在 onLocationChanged() 中,您编写 isBetterLocation(location, currentBestLocation)

isBetterLocation() 中,它将向您发送回 true< /code> 如果新位置比您现在拥有的最后一个位置更好(最后一个位置是 currentBestLocation

如果它返回 true,则意味着准确性很好..您应该使用该位置
(不要忘记更新 --> currentBestLocation = location

I found that the best way to obtain (and filter) the accuracy and fix location is to use the solution found at:
http://developer.android.com/guide/topics/location/obtaining-user-location.html#BestEstimate , and that's the BEST result I got so far.

Suppose you already have a good location and you make use of it (e.g. animateTo ..)
then let's suppose you save that location in Location variable and you call it currentBestLocation.

What you should do is put the isBetterLocation() (from "Maintaining a current best estimate"
I just posted in google site upward) method in your activity, and then when in your onLocationChanged()
you should send the currentBestLocation and the location you just got from the onLocationChanged() to isBetterLocation()
(e.g. from onLocationChanged() you write isBetterLocation(location, currentBestLocation).

in isBetterLocation() it will send you back true if the new location is better than the last location you have right now (the last location is currentBestLocation)

If it returns true, that means that the accuracy is good.. you should use that location
(don't forget to update --> currentBestLocation = location )

梨涡 2024-11-17 08:52:04
private class MyGPSListener implements GpsStatus.Listener {
    public void onGpsStatusChanged(int event) {
        switch (event) {
            case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                if (mLastLocation != null)
                    isGPSFix = (SystemClock.elapsedRealtime() - mLastLocationMillis) < 3000;

                if (isGPSFix) { // A fix has been acquired.
                    // Do something.
                } else { // The fix has been lost.
                    // Do something.
                }

                break;
            case GpsStatus.GPS_EVENT_FIRST_FIX:
                // Do something.
                isGPSFix = true;

                break;
        }
    }
}

好的,现在在 onLocationChanged() 中您应该添加以下内容:

@Override
public void onLocationChanged(Location location) {
    if (location == null) return;
    mLastLocationMillis = SystemClock.elapsedRealtime();
    // Do something.
    mLastLocation = location;
}
private class MyGPSListener implements GpsStatus.Listener {
    public void onGpsStatusChanged(int event) {
        switch (event) {
            case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                if (mLastLocation != null)
                    isGPSFix = (SystemClock.elapsedRealtime() - mLastLocationMillis) < 3000;

                if (isGPSFix) { // A fix has been acquired.
                    // Do something.
                } else { // The fix has been lost.
                    // Do something.
                }

                break;
            case GpsStatus.GPS_EVENT_FIRST_FIX:
                // Do something.
                isGPSFix = true;

                break;
        }
    }
}

OK, now in onLocationChanged() you should add the following:

@Override
public void onLocationChanged(Location location) {
    if (location == null) return;
    mLastLocationMillis = SystemClock.elapsedRealtime();
    // Do something.
    mLastLocation = location;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文