gps测试代码

import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.preference.PreferenceGroup;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.location.GpsStatus;
import android.location.GpsStatus.Listener;
import android.location.GpsSatellite;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;
import android.view.KeyEvent;

public class GpsTest extends PreferenceActivity {
    private static final String TAG = "GpsTest";

    private static final String KEY_SEARCHING_TIME = "key_searching_time";
    private static final String KEY_SATELLITES = "key_satellites";

    private Preference mSearchingTime = null;
    private PreferenceGroup mSatellites = null;
    private int stationNum;
	private int counter = 0;
    private Timer mTimer = null;
    private long mTimerCount = 0;
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            Log.d(TAG, "handleMessage msg.what = " + msg.what);
            if (msg.what == 1) {
                StringBuilder timeText = new StringBuilder();
                timeText.append(getString(R.string.gps_searching_time)).append(": ").append(mTimerCount).append("s");
                mSearchingTime.setTitle(timeText);
            } else {
                Log.d(TAG, "handleMessage invalid message");
            }
        }
    };
    
    private LocationManager mLocationManager = null;
    private LocationListener mLocationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            Log.v(TAG, "onLocationChanged");
            if (location != null) {
                double latitude = location.getLatitude();
                double longitude = location.getLongitude();
                Log.v(TAG, "onLocationChanged latitude = " + latitude + " longitude = " + longitude);
            } else {
                Log.v(TAG, "onLocationChanged location null");
            }
        }
        
        @Override
        public void onProviderDisabled(String provider) {
            Log.v(TAG, "onProviderDisabled provider = " + provider);
        }
        
        @Override
        public void onProviderEnabled(String provider) {
            Log.v(TAG, "onProviderEnabled provider = " + provider);
        }
        
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.v(TAG, "onStatusChanged provider = " + provider + "status = " + status);
        }
    };
    private GpsStatus mGpsStatus = null;
    private GpsStatus.Listener mGpsStatusListener = new GpsStatus.Listener() {
        @Override
        public void onGpsStatusChanged(int event) {
            Log.v(TAG, "onGpsStatusChanged event = " + event);
            mGpsStatus = mLocationManager.getGpsStatus(null);
            Log.v(TAG, "onGpsStatusChanged mGpsStatus = " + mGpsStatus);
            switch (event) {
                case GpsStatus.GPS_EVENT_FIRST_FIX:
                    Log.v(TAG, "onGpsStatusChanged GpsStatus.GPS_EVENT_FIRST_FIX");
                break;
                case GpsStatus.GPS_EVENT_STARTED:
                    Log.v(TAG, "onGpsStatusChanged GpsStatus.GPS_EVENT_STARTED");
                break;
                case GpsStatus.GPS_EVENT_STOPPED:
                    Log.v(TAG, "onGpsStatusChanged GpsStatus.GPS_EVENT_STOPPED");
                break;
                case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                    Log.v(TAG, "onGpsStatusChanged GpsStatus.GPS_EVENT_SATELLITE_STATUS");
                    Iterable<GpsSatellite> satelliteList = mGpsStatus.getSatellites();   
                    Iterator<GpsSatellite> iter = satelliteList.iterator(); 
                    mSatellites.removeAll();
                    int counter = 0;                                                                                                while (iter.hasNext()) {  
                        counter++;
                        GpsSatellite sat = (GpsSatellite)iter.next();
                        int prn = sat.getPrn();
                        float snr = sat.getSnr();
                        float elevation = sat.getElevation();
                        float azimuth = sat.getAzimuth();
                        Log.v(TAG, "onGpsStatusChanged GpsStatus.GPS_EVENT_SATELLITE_STATUS prn = " + prn + " snr = " + snr + " elevation = " + elevation + " azimuth = " + azimuth);
                        Preference preference = new Preference(GpsTest.this);
                        StringBuilder title = new StringBuilder();
                        title.append("Satellite ").append(counter);
                        preference.setTitle(title);
                        StringBuilder summary = new StringBuilder();
                        summary.append("PRN: ").append(prn).append(", ")
                            .append("SNR: ").append(snr).append(", ")
                            .append("Elevation: ").append(elevation).append(", ")
                            .append("Azimuth: ").append(azimuth);
                        preference.setSummary(summary);
                        mSatellites.addPreference(preference);
                    }
                break;
                default:
                break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.v(TAG, "onCreate");
        super.onCreate(savedInstanceState);

        mTimer = null;
        mTimerCount = 0;
        stationNum = getIntent().getIntExtra(
			AutoDeviceTestActivity.EXTRA_STATION_NUM, -1);

        this.addPreferencesFromResource(R.layout.gps_test);
        mSearchingTime = (Preference)findPreference(KEY_SEARCHING_TIME);
        mSatellites = (PreferenceGroup)findPreference(KEY_SATELLITES);
        
        StringBuilder timeText = new StringBuilder();
        timeText.append(getString(R.string.gps_searching_time)).append(": ").append(mTimerCount).append("s");
        mSearchingTime.setTitle(timeText);
        mSearchingTime.setSummary(null);

        mLocationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
        Log.v(TAG, "onCreate mLocationManager = " + mLocationManager);
        String currentProvider = mLocationManager.getProvider(LocationManager.GPS_PROVIDER).getName();
        Log.v(TAG, "onCreate currentProvider = " + currentProvider);
        Location currentLocation = mLocationManager.getLastKnownLocation(currentProvider);
        if (currentLocation == null) {
            mLocationManager.requestLocationUpdates(currentProvider, 1000, 0, mLocationListener);
            Log.v(TAG, "onCreate requestLocationUpdates");
        }
        
        mLocationManager.addGpsStatusListener(mGpsStatusListener);

        mTimer = new Timer();
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                Log.v(TAG, "run mTimerCount = " + mTimerCount);
                mTimerCount++;
                Message msg = mHandler.obtainMessage(1);
                mHandler.sendMessage(msg);
            }
        }, 1000, 1000);
    }
    
    @Override
    protected void onResume() {
        Log.v(TAG, "onResume");
        super.onResume();
       	//Native.chageGpsPower(1);
    }

    @Override
    protected void onPause() {
        Log.v(TAG, "onPause");
        super.onPause();
       	//Native.chageGpsPower(0);
    }

    @Override
    protected void onDestroy() {
        Log.v(TAG, "onDestroy");
        super.onDestroy();
        mLocationManager.removeUpdates(mLocationListener);
        mLocationManager.removeGpsStatusListener(mGpsStatusListener);

        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
            mTimerCount = 0;
        } 
    }
	public boolean onKeyUp(int keyCode, KeyEvent event){
	if (keyCode == KeyEvent.KEYCODE_BACK){
		finish();
	  }
	  return super.onKeyUp(keyCode, event);
   }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值