locationManager.getLastKnownLocation() return null

后端 未结 5 1217
天命终不由人
天命终不由人 2020-12-16 13:32

i dont understand why locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); return the location null. I gave all permission but its reutning

5条回答
  •  天命终不由人
    2020-12-16 13:59

    I used this method for get location i think it will help you

    private void startReceivingLocationUpdates() {
    
        if (mLocationManager == null) {
    
            mLocationManager = (android.location.LocationManager)
                    mContext.getSystemService(Context.LOCATION_SERVICE);
    
        }
    
        if (mLocationManager != null) {
    
            try {
    
                mLocationManager.requestLocationUpdates(
    
                        android.location.LocationManager.NETWORK_PROVIDER,
                        1000,
                        0F,
                        mLocationListeners[1]);
    
            } 
         catch (SecurityException ex) 
             {
                Log.i(TAG, "fail to request location update, ignore", ex);
    
            } 
    
           catch (IllegalArgumentException ex)
           {
                Log.d(TAG, "provider does not exist " + ex.getMessage());
            }
    
            try {
    
                mLocationManager.requestLocationUpdates(
    
                        android.location.LocationManager.GPS_PROVIDER,
                        1000,
                        0F,
                        mLocationListeners[0]);
    
                if (mListener != null) mListener.showGpsOnScreenIndicator(false);
    
    
            }
           catch (SecurityException ex) {
    
                Log.i(TAG, "fail to request location update, ignore", ex); } 
    
            catch (IllegalArgumentException ex) {
    
                Log.d(TAG, "provider does not exist " + ex.getMessage());  }
    
            Log.d(TAG, "startReceivingLocationUpdates");
        }
    }
    

提交回复
热议问题