• 同事介绍的新Gps Demo


    package com.xmb.MyGps2;
    
    import android.app.Activity;
    import android.util.Log;
    import android.os.Bundle;
    import android.location.GpsStatus;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.location.GpsSatellite;
    import android.widget.TextView;
    import android.content.Context;
    import java.util.Iterator;
    
    public class MyGps2 extends Activity {
        LocationManager mLocationManager;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            String provider = mLocationManager.GPS_PROVIDER;
            Location location = mLocationManager.getLastKnownLocation(provider);
            mLocationManager.requestLocationUpdates(provider, 4000, 10,
                    locationListener);
            mLocationManager.addGpsStatusListener(statusListener);
            updateWithNewLocation(location);
            updateGpsStatus(4, null);
        }
    
        public void onDestroy() {
            super.onDestroy();
        }
    
        private final GpsStatus.Listener statusListener = new GpsStatus.Listener() {
            public void onGpsStatusChanged(int event) {
                Log.w("out", "now gps status changed" + event);
                GpsStatus status = mLocationManager.getGpsStatus(null);
                updateGpsStatus(event, status);
            }
        };
    
        private final LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                Log.w("out", "now location changed");
                updateWithNewLocation(location);
            }
    
            public void onProviderDisabled(String provider) {
                Log.w("out", "now provider disable");
                updateWithNewLocation(null);
            }
    
            public void onProviderEnabled(String provider) {
                Log.w("out", "now provider enable");
            }
    
            public void onStatusChanged(String provider, int status, Bundle extras) {
                Log.w("out", "now provider status changed" + status);
            }
        };
    
        private void updateGpsStatus(int event, GpsStatus status) {
            TextView slView = (TextView) findViewById(R.id.tv1);
            if (status == null) {
                slView.setText("Satellites:" + "0");
            } else if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
                int maxSatellites = status.getMaxSatellites();
                Iterator<GpsSatellite> it = status.getSatellites().iterator();
                int count = 0;
                while (it.hasNext() && count <= maxSatellites) {
                    GpsSatellite s = it.next();
                    count++;
                }
                slView.setText("Satellites:" + count);
            }
        }
    
        private void updateWithNewLocation(Location location) {
            if (location != null) {
                double lat = location.getLatitude();
                double lng = location.getLongitude();
                TextView latView = (TextView) findViewById(R.id.tv1);
                TextView lngView = (TextView) findViewById(R.id.tv2);
                latView.setText("longitude"
                        + String.format("%.5f", lat));
                lngView.setText("latitude"
                        + String.format("%.5f", lng));
            } else {
                TextView latView = (TextView) findViewById(R.id.tv1);
                TextView lngView = (TextView) findViewById(R.id.tv2);
                latView.setText("cannot get geo info");
                lngView.setText("");
            }
        }
    }
  • 相关阅读:
    链表相加
    Unity 摄像机跟随
    整数反转
    两数和
    频繁项集挖掘思路
    有关于二进制的乘法计算(原码一位乘)
    JAVA面向对象(下)
    JAVAAPI
    JAVA面向对象()上)
    JAVA基础第一章
  • 原文地址:https://www.cnblogs.com/xmb7/p/3012516.html
Copyright © 2020-2023  润新知