检索#在GPS使用的卫星从Android的修复 [英] Retrieving # of satellites used in gps fix from Android

查看:134
本文介绍了检索#在GPS使用的卫星从Android的修复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找回在GPS定位卫星所使用的#。我已经实现两种不同的方法,如下所示:

 包ti.utils;

进口android.app.Activity;
进口android.content.Context;
进口android.location.GpsSatellite;
进口android.location.GpsStatus;
进口android.location.Location;
进口android.location.LocationListener;
进口android.location.LocationManager;
进口android.os.Bundle;

公共类全球定位系统{

    私有静态最终诠释TWO_MINUTES = 1000 * 60 * 2;
    私有静态位置位置;
    公共静态INT卫星= -1;

    公共静态无效StartTracking(活动活动)
    {
        最后LocationManager locationManager =(LocationManager)activity.getSystemService(Context.LOCATION_SERVICE);

        //监听GPS状态的变化。
        GpsStatus.Listener gpsStatusListener =新GpsStatus.Listener(){
            公共无效onGpsStatusChanged(INT事件){
                登录(在onGpsStatusChanged事件:+活动);
                如果(事件== GpsStatus.GPS_EVENT_SATELLITE_STATUS ||事件== GpsStatus.GPS_EVENT_FIRST_FIX){
                    GpsStatus状态= locationManager.getGpsStatus(空);
                    可迭代< GpsSatellite> SATS = status.getSatellites();
                    //检查卫星的数量列表来确定修复状态
                    卫星= 0;
                    对于(GpsSatellite坐:SATS){
                        //如果(sat.usedInFix())
                        卫星++;
                    }
                    日志(从GpsStatusListener设置卫星:+卫星);
                }
            }
        };
        locationManager.addGpsStatusListener(gpsStatusListener);

        //监听位置的变化。
        LocationListener的LocationListener的=新LocationListener的(){
            公共无效onLocationChanged(位置定位){
                //当一个新的位置由网络位置提供商发现调用。
                尝试
                {
                    日志(位置改变速度=!+ location.getSpeed​​()+&放大器;卫星=+ location.getExtras()调用getInt(卫星));
                    布尔isBetter = isBetterLocation(位置,位置);
                    如果(isBetter)
                    {
                        日志(设置为新的位置);
                        位置=位置;
                        卫星= location.getExtras()调用getInt(卫星)。
                        日志(从LocationListener的设置卫星:+卫星);
                    }
                }
                赶上(例外EXC)
                {
                    日志(exc.getMessage());
                }
            }
            公共无效onStatusChanged(字符串商,INT地位,捆绑演员){}
            公共无效onProviderEnabled(字符串提供商){}
            公共无效onProviderDisabled(字符串提供商){}
        };
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,LocationListener的);
    }

    / **确定一个位置读数是否比当前位置锁定更好
      * @参数的位置,你要评估新位置
      *参数currentBestLocation目前的位置锁定,您要比较新的这
      * /
    受保护的静态布尔isBetterLocation(地点位置,位置currentBestLocation){
        如果(currentBestLocation == NULL){
            //一个新的位置总是比没有位置更好
            返回true;
        }

        //检查新的位置锁定是否是新还是旧,
        长timeDelta = location.getTime() -  currentBestLocation.getTime();
        布尔isSignificantlyNewer = timeDelta> TWO_MINUTES;
        布尔isSignificantlyOlder = timeDelta< -TWO_MINUTES;
        布尔isNewer = timeDelta> 0;

        //如果它因为目前的位置已经超过两分钟,使用新位置
        //因为用户可能已经移动
        如果(isSignificantlyNewer){
            返回true;
        //如果新位置是超过两分钟以上,一定会更糟
        }否则,如果(isSignificantlyOlder){
            返回false;
        }

        //检查新的位置锁定是否或多或少准确
        INT accuracyDelta =(int)的(location.getAccuracy() -  currentBestLocation.getAccuracy());
        布尔isLessAccurate = accuracyDelta> 0;
        布尔isMoreAccurate = accuracyDelta< 0;
        布尔isSignificantlyLessAccurate = accuracyDelta> 200;

        //检查旧的和新的位置都来自同一个供应商
        布尔isFromSameProvider = isSameProvider(location.getProvider(),
                currentBestLocation.getProvider());

        //确定位置用质量及时性和准确性的结合
        如果(isMoreAccurate){
            返回true;
        }否则如果(isNewer&安培;&安培;!isLessAccurate){
            返回true;
        }否则如果(isNewer&安培;&安培;!isSignificantlyLessAccurate和放大器;&安培; isFromSameProvider){
            返回true;
        }
        返回false;
    }

    / **两个提供检查是否相同* /
    私有静态布尔isSameProvider(字符串provider1,字符串Provider2的){
        如果(provider1 == NULL){
          返回Provider2的== NULL;
        }
        返回provider1.equals(Provider2的);
    }

    私有静态无效日志(字符串消息){
        如果(消息=空&安培;!&安培; message.length()大于0){
            //android.util.Log.i(LCAT,消息);
            org.appcelerator.kroll.common.Log.i(SygicModule,邮件);
        }
    }

}
 

(onGpsStatusChanged&安培; onLocationChanged)

无论是监听器事件的不断激发,即使我有我的手机上启用了独立的GPS。我有ACCESS_FINE_LOCATION权限集:

 <使用-权限的Andr​​oid:名称=android.permission.ACCESS_FINE_LOCATION/>
 

我下载了所谓的GPS状态第三方应用程序,它能够正确显示卫星#,所以我知道这是不是一个问题,我的Droid X2。

任何想法?

解决方案

  locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,LocationListener的);
 

  locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,LocationListener的);
 

I am trying to retrieve the # of satellites used in the GPS fix. I have implemented two different methods, as shown below:

package ti.utils;

import android.app.Activity;
import android.content.Context;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class Gps {

    private static final int TWO_MINUTES = 1000 * 60 * 2;
    private static Location Location;
    public static int Satellites = -1;

    public static void StartTracking(Activity activity)
    {
        final LocationManager locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);

        //listen for gps status changes.
        GpsStatus.Listener gpsStatusListener = new GpsStatus.Listener() {
            public void onGpsStatusChanged(int event) {
                Log("In onGpsStatusChanged event: " + event);
                if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS || event == GpsStatus.GPS_EVENT_FIRST_FIX) {
                    GpsStatus status = locationManager.getGpsStatus(null);
                    Iterable<GpsSatellite> sats = status.getSatellites();
                    // Check number of satellites in list to determine fix state
                    Satellites = 0;
                    for (GpsSatellite sat : sats) {
                        //if(sat.usedInFix())
                        Satellites++;
                    }
                    Log("Setting Satellites from GpsStatusListener: " + Satellites);
                }
            }
        };
        locationManager.addGpsStatusListener(gpsStatusListener);

        //listen for location changes.
        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                // Called when a new location is found by the network location provider.
                try
                {
                    Log("Location changed! Speed = " + location.getSpeed() + " & Satellites = " + location.getExtras().getInt("satellites"));
                    boolean isBetter = isBetterLocation(location, Location);
                    if(isBetter)
                    {
                        Log("Set to new location");
                        Location = location;
                        Satellites = location.getExtras().getInt("satellites");
                        Log("Setting Satellites from LocationListener: " + Satellites);
                    }
                }
                catch(Exception exc)
                {
                    Log(exc.getMessage());
                }
            }
            public void onStatusChanged(String provider, int status, Bundle extras) {}
            public void onProviderEnabled(String provider) {}
            public void onProviderDisabled(String provider) {}
        };
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    }

    /** Determines whether one Location reading is better than the current Location fix
      * @param location  The new Location that you want to evaluate
      * @param currentBestLocation  The current Location fix, to which you want to compare the new one
      */
    protected static boolean isBetterLocation(Location location, Location currentBestLocation) {
        if (currentBestLocation == null) {
            // A new location is always better than no location
            return true;
        }

        // Check whether the new location fix is newer or older
        long timeDelta = location.getTime() - currentBestLocation.getTime();
        boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
        boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
        boolean isNewer = timeDelta > 0;

        // If it's been more than two minutes since the current location, use the new location
        // because the user has likely moved
        if (isSignificantlyNewer) {
            return true;
        // If the new location is more than two minutes older, it must be worse
        } else if (isSignificantlyOlder) {
            return false;
        }

        // Check whether the new location fix is more or less accurate
        int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
        boolean isLessAccurate = accuracyDelta > 0;
        boolean isMoreAccurate = accuracyDelta < 0;
        boolean isSignificantlyLessAccurate = accuracyDelta > 200;

        // Check if the old and new location are from the same provider
        boolean isFromSameProvider = isSameProvider(location.getProvider(),
                currentBestLocation.getProvider());

        // Determine location quality using a combination of timeliness and accuracy
        if (isMoreAccurate) {
            return true;
        } else if (isNewer && !isLessAccurate) {
            return true;
        } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
            return true;
        }
        return false;
    }

    /** Checks whether two providers are the same */
    private static boolean isSameProvider(String provider1, String provider2) {
        if (provider1 == null) {
          return provider2 == null;
        }
        return provider1.equals(provider2);
    }

    private static void Log(String message) {
        if(message != null && message.length() > 0) {
            //android.util.Log.i(LCAT, message);
            org.appcelerator.kroll.common.Log.i("SygicModule", message);
        }
    }

}

Neither of the listener events (onGpsStatusChanged & onLocationChanged) ever fire, even though I have standalone GPS enabled on my phone. I have the ACCESS_FINE_LOCATION permission set:

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

I downloaded a 3rd party app called GPS Status and it was able to correctly display the # of satellites, so I know it isn't an issue with my Droid X2.

Any ideas?

解决方案

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

Should be

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

这篇关于检索#在GPS使用的卫星从Android的修复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆