简体   繁体   中英

How to resume Location access programmatically when app open

I developed a GPS app but it works correctly when location service turns off and again turn on. When getting GPS without location service turn off and again turn on GPS ,latitude and longitude are not correct. How can I fix it?

public void onLocationChanged(Location location) {

    try {

        latitute = location.getLatitude();
        longitude = location.getLongitude();
        accuracy = location.getAccuracy();
        Provider = location.getProvider();

        Toast.makeText(getContext(), "onLocationChanged: " + "Lat: " + latitute + "Lon: " + longitude, Toast.LENGTH_SHORT).show();

        latituteField.setText(String.valueOf((double) latitute));
        longitudeField.setText(String.valueOf((double) longitude));
        txtaccuracy.setText(String.valueOf((double) accuracy));
        txtprovider.setText(String.valueOf(Provider));

    } catch (Exception e) {

    }

Check in your onResume() method of your activity that Gps is enabled or not. if Gps is not enabled then ask user to enable GPS/network in settings. after Gps enable start your GPSTracker service to get location.

 @Override
protected void onResume() {
    super.onResume();
   // Check if GPS is not enabled
    if (!canGetLocation) {
        // GPS or network is not enabled.
        // Ask user to enable GPS/network in settings.

    }
}

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