从打开位置导航回来后如何获取坐标?



一旦您从用于打开位置的意图中返回的意图,是获得getCoordinates方法运行的最佳方法。当您打开位置后,背压后,活动lifecylce中会发生什么,以及一旦您从打开位置返回的位置,或者在位置返回时,在哪里拨打GetCoordinates方法,或者首先检查位置是否在只有然后运行getCoordinates方法?

checklocationon

public void checkLocationOn () {
    LocationManager lm = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
    boolean gps_enabled = false;
    boolean network_enabled = false;
    try {
        gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch(Exception ex) {}
    try {
        network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch(Exception ex) {}
    if(!gps_enabled && !network_enabled) {
        // notify user
        AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
        dialog.setCancelable(false);
        dialog.setTitle("Location Needed");
        dialog.setMessage("The devices location settings are not enabled, please enable it in order to use this application...");
        dialog.setPositiveButton("Open Location Settings", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(myIntent);
             }
        });
        dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                // TODO Auto-generated method stub
            }
        });
        dialog.show();
    }
}

getCoordinates

 public void getCoordinates() {
    if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    mFusedLocationClient.getLastLocation()
            .addOnSuccessListener(getActivity(), new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {
                     lat = location.getLatitude();
                     lng = location.getLongitude();
                    latitude = Double.toString(lat);
                    longitude = Double.toString(lng);
                    SharedPreferences.Editor e = preferences.edit();
                    e.putString("myLats",latitude);
                    e.putString("myLngs",longitude);
                    e.commit();
                }
            });
}

oncreateview

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    v = inflater.inflate(R.layout.job_search_fragment, container, false);
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getActivity());
        checkLocationOn();
        getCoordinates();
        //Please help here

    return v;
}

覆盖onResume并从那里调用checkLocationOn();getCoordinates();

如果您请求连续的位置更新,则可以在onPause方法中删除请求。

谢谢Arsena,我刚将CheckLocationOn()更改为布尔值,然后在OnResume()中,如果CheckLocationon()为true,则我将GetCoorceartion()称为so。

public boolean checkLocationOn () {
    LocationManager lm = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
    boolean gps_enabled = false;
    boolean network_enabled = false;
    try {
        gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch(Exception ex) {}
    try {
        network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch(Exception ex) {}
    if(!gps_enabled && !network_enabled) {
        // notify user
        final AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
        dialog.setCancelable(false);
        dialog.setTitle("Location Needed");
        dialog.setMessage("The devices location settings are not enabled, please enable it in order to use this application...");
        dialog.setPositiveButton("Open Location Settings", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(myIntent);
            }
        });
        dialog.show();
        return false;
    }else{
        return true;
    }
}

然后在onresume()

@Override
public void onResume() {
    super.onResume();
    if(checkLocationOn()){
        getCoordinates();
    }
}

现在工作

最新更新