简体   繁体   中英

Locationmanager getLastKnownLocation always null

i have a problem. When i test this. and i ask for the showCurrentLocation function it always returns null. It works in the emulator when i send the location after. But i need this to work on the phone, and there you can't send the location like in de DDNS window.

Here's my code

public class LbsGeocodingActivity extends Activity {

private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1; // in Milliseconds

protected LocationManager locationManager;

protected Button retrieveLocationButton;
protected Button stopLocationButton;


@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            MINIMUM_TIME_BETWEEN_UPDATES, 
            MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
            new MyLocationListener()
    );

    retrieveLocationButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            showCurrentLocation();
        }
    });  
    /*stopLocationButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //locationManager.removeUpdates(MyLocationListener)  ;          
        }
    }); */ 

}    

public String getMyPhoneNumber(){
    TelephonyManager mTelephonyMgr;
    mTelephonyMgr = (TelephonyManager)
    getSystemService(Context.TELEPHONY_SERVICE); 
    return mTelephonyMgr.getLine1Number();
}

protected void showCurrentLocation() {

    Criteria crit = new Criteria();
    crit.setAccuracy(Criteria.ACCURACY_FINE);
    String provider = locationManager.getBestProvider(crit, true);
    Location loc = locationManager.getLastKnownLocation(provider);


    if (loc != null) {
        String longi = "" + loc.getLongitude();
        String lat = "" + loc.getLatitude();
        String num = getMyPhoneNumber();
        String message = String.format(
                "Current Location \n  Longitude: %1$s \n Latitude: %2$s \n %3$s ",
                longi,
                lat,
                num         );
        Toast.makeText(LbsGeocodingActivity.this, message,
                Toast.LENGTH_LONG).show();
    }
    if (loc == null)Toast.makeText(LbsGeocodingActivity.this, "Null ",
            Toast.LENGTH_LONG).show();
}   

private class MyLocationListener implements LocationListener {



    public void onLocationChanged(Location loc) {
        String longi = "" + loc.getLongitude();
        String lat = "" + loc.getLatitude();
        String num = getMyPhoneNumber();
        String message = String.format(
                "New Location \n Longitude: %1$s \n Latitude: %2$s \n %3$s ",
                longi,
                lat,
                num                 );
        Toast.makeText(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show();
    }

    public void onStatusChanged(String s, int i, Bundle b) {
        Toast.makeText(LbsGeocodingActivity.this, "Provider status changed",
                Toast.LENGTH_LONG).show();
    }

    public void onProviderDisabled(String s) {
        Toast.makeText(LbsGeocodingActivity.this,
                "Provider disabled by the user. GPS turned off",
                Toast.LENGTH_LONG).show();
    }

    public void onProviderEnabled(String s) {
        Toast.makeText(LbsGeocodingActivity.this,
                "Provider enabled by the user. GPS turned on",
                Toast.LENGTH_LONG).show();
    }

}

}

My permissions are: FINE_LOCATION COURSE_LOCATION.

I really just want it that it tracks the users location, even on the background..

You can sent mock locations also to your Android device see Android mock location on device?

Then besides that I had this problem too it seems it maybe never had a location before on your device try to open google maps and make sure you get located and then try again. Also I suspected something like first time you use the application you don't have access to a last know location yet because you never used it so you first need to get located and next time you startup the application it will work. If you want a quick location try to get located by wifi or cell towers

And make sure the permissions are set!

您是否已检查手机中的GPS是否已开启,但是在开启GPS后,将需要一些时间来获取位置更新。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM