手机重启后Android获取位置为空

标签 android geolocation android-location

在我的应用程序中,我通过这个函数获取手机的位置,但是当我重启手机并启动应用程序时,我从这个方法中得到了 null。我有什么遗漏或做错了什么吗?我应该怎么做才能解决这个问题?

这是我使用的函数:

public void getAddress() {
        Log.v("--", "get address 1");
        boolean isGPSProviderEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean network_enabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        Log.v("--", "get address 31 " + isGPSProviderEnabled + " gps  -  "
                + isConnectedToNetwork());

        if (isGPSProviderEnabled || network_enabled) {
            Log.v("--", "get address 2");
            Criteria c = new Criteria();
            Log.v("--", "provider " + locationManager.getBestProvider(c, true));
            locationManager.requestSingleUpdate(
                    locationManager.getBestProvider(c, true),
                    mLocationListener, Looper.myLooper());
            location = locationManager.getLastKnownLocation(locationManager
                    .getBestProvider(c, true));
            if (location == null) {
                Log.v("--", "get address 6");
                // TODO check if this is working
                locationManager.requestSingleUpdate(
                        locationManager.getBestProvider(c, true),
                        mLocationListener, Looper.myLooper());
                locationManager.requestLocationUpdates(
                        locationManager.getBestProvider(c, true), 0, 0,
                        mLocationListener);
                Location oldLocation = new Location("");
                oldLocation.setLatitude(new Double(prefs.getString(
                        Constants.LATITUDE, "48.51")));
                oldLocation.setLongitude(new Double(prefs.getString(
                        Constants.LONGITUDE, "2.20")));
                populateList(oldLocation);
                // locationManager.requestLocationUpdates(
                // locationManager.getBestProvider(c, true), 1000, 100,
                // mLocationListener);
            } else {
                Log.v("--", "get address 3");
                if (isConnectedToNetwork()) {
                    new AsyncTask<Void, Void, Void>() {
                        protected Void doInBackground(Void... params) {
                            try {
                                com.quanticapps.athan.utils.Geocoder geocoder = new com.quanticapps.athan.utils.Geocoder(
                                        Main.this);
                                GeocoderModel geocoderModel = geocoder
                                        .getFromLocation(
                                                location.getLatitude(),
                                                location.getLongitude(), 5);
                                city = geocoderModel.getCity();
                                country = geocoderModel.getCountry();
                                prefs.edit().putString(Constants.CITY, city)
                                        .apply();
                                Log.v("--", "get address 4");
                            } catch (IOException e) {
                                Log.v("--", "get address 11");
                                e.printStackTrace();
                            } catch (LimitExceededException e) {
                                Log.v("--", "get address 12");
                                e.printStackTrace();
                            }
                            return null;
                        };

                        protected void onPostExecute(Void result) {
                            prefs.edit().putString(Constants.COUNTRY, country)
                                    .apply();
                            prefs.edit().putString(Constants.CITY, city)
                                    .apply();
                            populateList(location);
                        };
                    }.execute();
                } else {
                    city = null;
                    Log.v("--", "get address 33 " + location.getLatitude());
                    populateList(location);
                }
            }
        } else {
            Log.v("--", "get address 5");
            startGpsEnableDialog();
        }

    }

和我的位置监听器:

private final LocationListener mLocationListener = new LocationListener() {
        @Override
        public void onLocationChanged(final Location location) {
            // TODO
            Log.v("--", "get address 121");
            if (location != null) {
                Main.this.location = location;
                getAddress();
            }
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
            Log.v("--", "provider enabled");
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
            Log.v("--", "provider disabled");
        }
    };

最佳答案

当手机重新启动时,缓存的最后位置将丢失,因此如果您没有打开使用 GPS 的应用程序(如谷歌地图或其他应用),则不会有最后位置。

永远不会有一个位置返回给你,你应该总是假设它可能是空的

关于手机重启后Android获取位置为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28173322/

相关文章:

Android - 更改 Holo 主题默认蓝色

android - 不可阻挡的 Android 服务正在监听地理定位

android - 如何让地理围栏精准?

android - 非常简单的Android地理围栏示例

android - 连接管理器 - android.net.conn.CONNECTIVITY_CHANGE

android - ANKO paddingTop - Val 无法重新分配

java - 无法让 Android 应用程序退出

android - 使用 Camera Intent 后对照片进行地理标记

javascript - 如何使用位置感知地理定位在 Web 应用程序上查找本地企业?