您当前的位置:首页 > 计算机 > 编程开发 > 安卓(android)开发

如果LocationListener中onStatusChanged无法返回实时GPS定位状态可以通过卫星已定位辅助判断

时间:04-07来源:作者:点击数:

代码如下:

package dls.tbox.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;

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;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

public abstract class GPSObserver {
    public static final String TAG = "TB_GPSObserver";
    public static final boolean LOG_ENABLED = true;
    public int mOldUseFix = 0;
    private Context mContext;
    private Workthread wt;

    public GPSObserver(Context context) {
        mContext = context;
        wt = new Workthread();
        wt.start();
    }

    private class Workthread extends Thread implements LocationListener {
        private LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        private String gpsProvider = LocationManager.GPS_PROVIDER;
        private Handler mHandler;
        private boolean paused = true;
        GpsStatus mGpsStatus;
        GpsStatus.Listener gpsStatusListener;
        Vector<GpsSatellite> vSatellite;

        private void resumeWork() {
            LOGD("resumeWork");
            synchronized (this) {
                paused = false;
                if (locationManager != null) {
                    locationManager.requestLocationUpdates(gpsProvider, 1, 0, this);

                   gpsStatusListener = new GpsStatus.Listener()
                   {
                       public void onGpsStatusChanged(int event)
                       {
                           mGpsStatus = locationManager.getGpsStatus(null);
                           switch (event)
                           {
                               case GpsStatus.GPS_EVENT_FIRST_FIX:
                                   LOGD("GpsTest::onCreate.gpsStatusListener"+ "GPS_EVENT_FIRST_FIX");
                                   mGpsStatus.getTimeToFirstFix();
                                   break;
                               case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                                   int totalStat = 0;
                                   int usedStat = 0;
                                   Iterable<GpsSatellite> gsiter = mGpsStatus.getSatellites();
                                   Iterator<GpsSatellite> it = gsiter.iterator();
                                   vSatellite = new Vector<GpsSatellite>();
                                   ArrayList<Float> snrarray = new ArrayList<Float>();
                                   while (it.hasNext()) {
                                       GpsSatellite sate = (GpsSatellite)(it.next());
                                       vSatellite.add(sate);
                                       float snr = sate.getSnr();
                                       snrarray.add(snr);
                                        totalStat++;
                                        if (sate.usedInFix()) {
                                            usedStat++;
                                        }
                                   }
                                   if(mOldUseFix != usedStat && usedStat > 0) {
                                       LOGD("+++++++++++gps fix from lost++++++mOldUseFix " + mOldUseFix + " usedStat is " + usedStat);
                                       mOldUseFix = usedStat;
                                   } else if(mOldUseFix != usedStat && usedStat == 0) {
                                       LOGD("++++++++++++gps lost from fix mOldUseFix " + mOldUseFix + " usedStat is " + usedStat);
                                       mOldUseFix = usedStat;
                                       wt.onStatusChanged(null, usedStat, null);
                                   }
                                   snrarray.clear();
                                   break;

                               case GpsStatus.GPS_EVENT_STARTED:
                                   LOGD("GpsTest::onCreate.gpsStatusListener"+ "getMaxSatellites()=");
                                   break;

                               case GpsStatus.GPS_EVENT_STOPPED:
                                   LOGD("GpsTest::onCreate.gpsStatusListener"+ "GPS_EVENT_STOPPED");
                                   break;
                           }
                       }
                   };
                   locationManager.addGpsStatusListener(gpsStatusListener);
               }
           }
    }

        private void pause() {
            synchronized (this) {
                paused = true;
                if (locationManager != null) {
                    locationManager.removeUpdates(this);
                    locationManager.removeGpsStatusListener(gpsStatusListener);
                }
            }
        }

        private void quit() {
            synchronized (this) {
                if (locationManager != null) {
                    locationManager.removeUpdates(this);
                }

                if (mHandler == null) {
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                mHandler.getLooper().quit();
            }
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
            if (!paused) {
                onGPSChangeLost(status);
            } else {
                LOGD("GPSObserver paused..");
            }
        }

        public void onProviderEnabled(String provider) {
        }

        public void onProviderDisabled(String provider) {
        }

        public void onLocationChanged(Location location) {
            if (!paused) {
                onGPSLocation(location);
            } else {
                LOGD("GPSObserver paused..");
            }
        }

        @Override
        public void run() {
            super.run();
            synchronized (this) {
                Looper.prepare();
                mHandler = new Handler();
                notify();
            }
            Looper.loop();
            LOGD("GPSObserver workthread quit...");
        }
    }

    public void start() {
        wt.resumeWork();
    }

    public void pause() {
        wt.pause();
    }

    public void stop() {
        pause();
        wt.quit();
    }

    public abstract void onGPSLocation(Location l);
    public abstract void onGPSChangeLost(int s);

    private static void LOGD(String msg) {
        if (LOG_ENABLED) {
            Log.d(TAG, msg);
        }
    }

    private static void LOGW(String msg) {
        if (LOG_ENABLED) {
            Log.w(TAG, msg);
        }
    }

    private static void LOGE(String msg) {
        if (LOG_ENABLED) {
            Log.e(TAG, msg);
        }
    }
}
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门