0
如何在啓用GPS設置後刷新地圖片段? 我使用下面的代碼來啓用GPS設置。啓用GPS設置不起作用後刷新地圖以顯示當前位置
//ask for runtime permission to showing map
private void askForPermission(String permission, Integer requestCode) {
if (ContextCompat.checkSelfPermission(getActivity(), permission) != PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), permission)) {
ActivityCompat.requestPermissions(getActivity(), new String[]{permission}, requestCode);
//getActivity().finish();
} else {
ActivityCompat.requestPermissions(getActivity(), new String[]{permission}, requestCode);
}
}
}
//check the gps is enable or not
public void statusCheck() {
manager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildAlertMessageNoGps();
}
}
//alert dialog to enable location permission
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}