zoukankan      html  css  js  c++  java
  • 同事介绍的新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("");
            }
        }
    }
  • 相关阅读:
    Codeforces 1131 C. Birthday-暴力 (Codeforces Round #541 (Div. 2))
    Codeforces 1131 B. Draw!-暴力 (Codeforces Round #541 (Div. 2))
    DP之背包经典三例
    博弈规律综概
    腾讯面试题:杯子质量(最优查找)
    洛谷P1308——单词统计
    AtCoder Regular Contest 069 D
    Codeforces 782C. Andryusha and Colored Balloons 搜索
    Codeforces 799D. String Game 二分
    Codeforces 767B. The Queue 模拟题
  • 原文地址:https://www.cnblogs.com/xmb7/p/3012516.html
Copyright © 2011-2022 走看看