Android 定位模板

Posted IT_lss

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
        }
    }

 

以上是关于Android 定位模板的主要内容,如果未能解决你的问题,请参考以下文章

在 Android Studio 的默认导航抽屉模板中使用按钮在片段之间切换

我的Android进阶之旅NDK开发之在C++代码中使用Android Log打印日志,打印出C++的函数耗时以及代码片段耗时详情

Android 定位模板

VSCode自定义代码片段——.vue文件的模板

如何修复空白片段?

VSCode自定义代码片段1——vue主模板