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("");
            }
        }
    }
  • 相关阅读:
    c语言中重要函数
    python 类属性、对象属性
    windows下PIP安装模块编码错误解决
    python爬取百思不得姐视频
    ubuntu下刷新dns
    pycharm设置安装python第三方插件
    python将str转换成字典
    pyqt加载图片
    Python端口扫描器
    自己构造用于异步请求的JSON数据
  • 原文地址:https://www.cnblogs.com/xmb7/p/3012516.html
Copyright © 2011-2022 走看看