位置をオンにするために使用されたインテントから戻ると、getCoordinatesメソッドを実行する最良の方法を教えてください。背番号をオンにした後で背中を動かすと、アクティビティのライフサイクルはどうなりますか?また、場所の切り替えから戻った場合に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;
}
作業= HTTPS://www.androidhive.info/2012/07/android-gps -location-manager-tutorial / –