current location is always null

后端 未结 6 1885
独厮守ぢ
独厮守ぢ 2021-02-04 22:39

I am trying to retrieve my current location on the button click in an editbox using gps or network..i have tried all possible methods which i found in tutorials and older posts.

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-04 22:56

    Try this Code:

    public CurrentLocation(Context ctx) {
        this.context = ctx;
    
        locationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        @SuppressWarnings("unused")
        List providers = locationManager.getAllProviders();
    
        Criteria locationCritera = new Criteria();
        String providerName = locationManager.getBestProvider(locationCritera,
                true);
    
        location = locationManager.getLastKnownLocation(providerName);
        locationListener = new MyLocationListener();
    
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                0, locationListener);
    
    
    
    }
    

提交回复
热议问题