【问题标题】:locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER) returns nulllocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER) 返回 null
【发布时间】:2016-01-16 12:28:40
【问题描述】:

我有一个定位应用,可以将我手机的位置发送到数据库。当我尝试通过网络(不开启 3G)获取位置时,它可以工作!但是当我打开我的 3G 时它不起作用,因为代码: locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

总是返回 NULL。这是我的完整位置处理程序..

我的 locationhandler 类的代码。

package info.androidhive.loginandregistration.location;

import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.util.Log;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class LocationHandler {

    Context mContext = null;
    Context ctx;
    double Lat;
    double Long;
    Location Localization;
    LocationManager locationManager;
    boolean GPSActivate = false;
    boolean GPRSActivate = false;
    boolean getUbicationbool;

    private double mLastLatitudeLocation = 0;
    private double mLastLongitudeLocation = 0;
    Intent intent;

    // flag for GPS status
    public boolean isGPSEnabled = false;

    // flag for network status
    boolean isNetworkEnabled = false;

    // flag for GPS status
    boolean canGetLocation = false;

    Location location; // location
    double latitude; // latitude
    double longitude; // longitude

    public LocationHandler(Context context) {

        mContext = context;
    }

    private void ConfigurationManager() {
        locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        // getting GPS status
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        Log.v("isGPSEnabled", "=" + isGPSEnabled);
        // getting network status
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        Log.v("isNetworkEnabled", "=" + isNetworkEnabled);
    }

    public Location getLocation() {
        try {

            ConfigurationManager();

            if (isGPSEnabled == false && isNetworkEnabled == false) {
                // no network provider is enabled
                return null;
            } else {
                this.canGetLocation = true;
                if (isNetworkEnabled) {
                    location = null;
                    Log.d("Network", "Network - GET LOCATION ");
                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
                if (isGPSEnabled) {
                    location = null;
                    if (location == null) {
                        Log.d("GPS Enabled", "GPS Enabled - GET LOCATION");
                        if (locationManager != null) {
                            location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return location;
    }
}

【问题讨论】:

    标签: android location


    【解决方案1】:

    1-您必须确保您在清单文件中添加了权限:

    ACCESS_FINE_LOCATION
    

    2- 根据 SDK 的 GPS 提供者是:“此提供者使用卫星确定位置。根据条件,此提供者可能需要一段时间才能返回位置修复。”这意味着你必须打开 GPS 而不是 3G

    【讨论】:

      猜你喜欢
      • 2014-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多