安卓 GPS连接信息工具类

package com.renbao.eliving.resource;

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

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.Message;
import android.util.Log;

/**
 * 说明:GPS连接信息
 */

public class GPSSatelliteInfor {
    private String TAG = "GPSTest";
    // private Context context;
    private LocationManager locationManager = null;
    private Handler handler = null;
    private double latitude;
    private double longitude;
    private float aziMuth;
    private float eleVation;
    private float snr;
    private int prn;
    private boolean connect_usedInFix;
    private int satelliteNum;
    private int satelliteConnectNum;
    private float speed;
    // private long time;
    private double height;

    public void setHandler(Handler handler){
        this.handler = handler;
    }    
    
    public double getLatitude(){
        return latitude;
    }
    
    public double getLongitude(){
        return longitude;
    }
    
    public int getSatelliteNum() {
        return satelliteNum;
    }

    public int getSatelliteConnectNum() {
        return satelliteConnectNum;
    }

    public float getSpeed() {
        return speed;
    }

    public double getHeight() {
        return height;
    }

    public GPSSatelliteInfor(Context context) {
        // this.context = context;
        createFunction(context);
    }
    
    public GPSSatelliteInfor(Context context,Handler handler){
        createFunction(context);
        this.handler = handler;
    }

    public void createFunction(Context context){
        locationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        String provider = LocationManager.GPS_PROVIDER;
        Location location = locationManager.getLastKnownLocation(provider);
        locationManager.requestLocationUpdates(provider, 5000, 0.5f,
                locationListener);
        locationManager.addGpsStatusListener(statusListener);

        aziMuth = 0.0f;
        eleVation = 0.0f;
        satelliteNum = 0;
        satelliteConnectNum = 0;
        speed = 0.00f;
        height = 0;

        // locationManager = (LocationManager)
        // getSystemService(Context.LOCATION_SERVICE);
        // location =
        // locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location != null) {
            speed = location.getSpeed(); // km/h
            height = (int) location.getAltitude(); // m
            Log.i(TAG, "speed" + speed);
            Log.i(TAG, "height" + height);
        } else {
            Log.i(TAG, "---------------无法获取地理信息------------------------");
        }
        updateWithNewLocation(location);
        updateGpsStatus(0, null);
    }
    
    
    private void updateGpsStatus(int event, GpsStatus status) {
        try {
            if (status == null) {
                satelliteNum = 0;
                satelliteConnectNum = 0;
            } else if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
                int maxSatellites = status.getMaxSatellites();

                Iterator<GpsSatellite> it = status.getSatellites().iterator();
                m_listStar.clear();
                int count = 0;
                int connect_count = 0;
                while (it.hasNext() && count <= maxSatellites) {
                    GpsSatellite s = it.next();
                    count++;
                    
                    aziMuth = s.getAzimuth();
                    eleVation = s.getElevation();
                    snr = s.getSnr();
                    prn = s.getPrn();
                    
                    connect_usedInFix = s.usedInFix();
                    Log.w(TAG, "-----连接卫星数为true吗------------" + connect_usedInFix);

                    if (connect_usedInFix == true) {
                        connect_count++;
                    }

                    SatelliteInfo satelliteInfo = new SatelliteInfo();
                    satelliteInfo.aziMuth = aziMuth;
                    satelliteInfo.eleVation = eleVation;
                    satelliteInfo.prn = String.valueOf(prn);
                    satelliteInfo.snr = snr;
                    m_listStar.add(satelliteInfo);

                    Log.i(TAG, "getAzimuth" + s.getAzimuth());
                    Log.i(TAG, "getElevation" + s.getElevation());
                    Log.w(TAG, "信噪比" + s.getSnr());
                    Log.w(TAG, "随机码" + s.getPrn());
                    
                    Log.i(TAG, "count:" + count);
                    Log.i(TAG, "connect_count:" + connect_count);
                }

                satelliteNum = count;
                satelliteConnectNum = connect_count;
                
                Log.i(TAG, "Number of satellite be connected:" + connect_count);
                
                Message msg = new Message();
                msg.what = 1;
                Bundle b = msg.getData();
                // 默认都可用
                b.putInt("satelliteNum", satelliteNum);
                b.putInt("satelliteConnectNum", satelliteConnectNum);

                // 发送消息
                if (handler != null) {
                    handler.sendMessage(msg);
                }
                

            }
        } catch (Exception e) {
            // TODO: handle exception
            ;
        }
    }

    private void updateWithNewLocation(Location location) {

        // TODO Auto-generated method stub
        if (location != null) {
            
            latitude=location.getLatitude();
            longitude=location.getLongitude();
            speed = location.getSpeed();
            height = (int) location.getAltitude();

            Log.i(TAG, "updateWithNewLocation时速" + speed);
            Log.i(TAG, "updateWithNewLocation海拔" + height);
            
            Message msg = new Message();
            msg.what = 2;
            Bundle b = msg.getData();
            // 默认都可用
            b.putDouble("latitude", latitude);
            b.putDouble("longitude", longitude);
            b.putFloat("speed", speed);
            b.putDouble("height", height);
            
            // 发送消息
            if (handler != null) {
                handler.sendMessage(msg);
            }
            
        } else {
            System.out.println("无法获取地理信息");
            Log.i(TAG, "---------------无法获取地理信息------------------------");
        }
    }

    
    public void destory() {
        try {
            if (locationManager != null) {
                locationManager.removeUpdates(locationListener);
                locationManager.removeGpsStatusListener(statusListener);
                
                locationManager = null;
            }
            
            handler = null;
        } catch (Exception e) {
            // TODO: handle exception
            ;
        }        
        
    }

    private final GpsStatus.Listener statusListener = new GpsStatus.Listener() {
        public void onGpsStatusChanged(int event) {
            Log.w(TAG, "now gps status changed" + event);
            if(locationManager!=null){
                GpsStatus status = locationManager.getGpsStatus(null);
                updateGpsStatus(event, status);
            }
        }
    };

    private final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            Log.w(TAG, "now location changed");
            updateWithNewLocation(location);
        }

        public void onProviderDisabled(String provider) {
            Log.w(TAG, "now provider disable");
            updateWithNewLocation(null);
            if (handler != null) {
                handler.sendEmptyMessage(-1);
            }
        }

        public void onProviderEnabled(String provider) {
            Log.w(TAG, "now provider enable");
            if (handler != null) {
                handler.sendEmptyMessage(0);
            }
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.w(TAG, "now provider status changed" + status);
        }
    };

    private class SatelliteInfo {
        float aziMuth;
        float eleVation;
        float snr;
        String prn;
    }
    
    ArrayList<SatelliteInfo> m_listStar = new ArrayList<SatelliteInfo>();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值