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("");
            }
        }
    }
  • 相关阅读:
    【Leetcode】【Easy】Remove Duplicates from Sorted List
    【Leetcode】【Easy】Pascal's Triangle II
    【Leetcode】【Easy】Pascal's Triangle
    【Leetcode】【Easy】Binary Tree Level Order Traversal II
    【Leetcode】【Easy】Binary Tree Level Order Traversal
    【Leetcode】【Easy】Maximum Depth of Binary Tree
    【Leetcode】【Easy】Minimum Depth of Binary Tree
    【Leetcode】【Easy】Balanced Binary Tree
    【Leetcode】【Easy】Symmetric Tree
    如何使用Action.Invoke()触发一个Storyboard
  • 原文地址:https://www.cnblogs.com/xmb7/p/3012516.html
Copyright © 2011-2022 走看看