zoukankan      html  css  js  c++  java
  • Android 定位模板

    private void getLocationCity(){
    
            String serviceName = Context.LOCATION_SERVICE;
            locationManager = (LocationManager) getSystemService(serviceName);
    //        String provider = LocationManager.GPS_PROVIDER;
            String provider = LocationManager.NETWORK_PROVIDER;
    
            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            criteria.setAltitudeRequired(false);
            criteria.setBearingRequired(false);
            criteria.setCostAllowed(true);
            criteria.setPowerRequirement(Criteria.POWER_LOW);
    //        String provider = locationManager.getBestProvider(criteria, true);
         locationManager.requestLocationUpdates(provider, 2000, 10,
                    locationListener);
            Location location = locationManager.getLastKnownLocation(provider);
            getAddress(location);   
        }
    
        private final LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                getAddress(location);
            }
            public void onProviderDisabled(String provider){
                getAddress(null);
            }
            public void onProviderEnabled(String provider){ }
            public void onStatusChanged(String provider, int status,
                                        Bundle extras){ }
        };
    
        private void getAddress(Location location) {
    
            if (location != null) {
                new MyAsyncCity().execute(location);
            } else {
                Log.d(TAG, "updateWithNewLocation: ");
            }
        }
    
        private class MyAsyncCity extends AsyncTask<Location, Void, String> {
    
            @Override
            protected String doInBackground(Location... params) {
                List<Address> addList = null;
                Geocoder ge = new Geocoder(WelcomeActivity.this);
                try {
                    addList = ge.getFromLocation(params[0].getLatitude(), params[0].getLongitude(), 1);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return addList.get(0).getLocality();
            }
    
            @Override
            protected void onPostExecute(String city) {
                super.onPostExecute(city);
                spUtil.setLocationCity(city);
            }
        }
  • 相关阅读:
    组合博弈入门
    模拟练1
    鼠标点击 input,显示瞬间的边框颜色,对之修改与隐藏
    display: inline-block兼容性写法
    background-clip与background-origin两者的区别
    article标签和aside标签两者的理解
    jQuery插件实现左右无缝轮播
    JS面向对象基础2
    JS面向对象基础1
    CSS3的基础知识点
  • 原文地址:https://www.cnblogs.com/IT-lss/p/8350981.html
Copyright © 2011-2022 走看看