私はモバイルの場所を無効にしましたが、位置情報はまだトーストで許可されています。実行時にアクセス許可を要求Android Marshmallowが動作しない
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(Build.VERSION.SDK_INT>=23){
showRunTimePermission();
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_search_v2, container,
false);
return view;
}
//ロケーションが無効の場合は警告ダイアログを表示します。
private void showRunTimePermission() {
if(ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION)!=PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)!=PackageManager.PERMISSION_GRANTED){
requestLocationPermission();
}else{
Toast.makeText(getActivity(), "Location already granted", Toast.LENGTH_SHORT).show();
}
}
private void requestLocationPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),Manifest.permission.ACCESS_COARSE_LOCATION) || ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),Manifest.permission.ACCESS_FINE_LOCATION)){
final AlertDialog locationDialog = new AlertDialog.Builder(getActivity())
.setCancelable(false).create();
View dialogView = View.inflate(getActivity(), R.layout.dialog_location_access, null);
TextView okBtn= (TextView) dialogView.findViewById(R.id.dialog_location_ok_button);
okBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ActivityCompat
.requestPermissions(getActivity(), PERMISSIONS_LOCATION,
LOCATION_PERMISSIONS_REQUEST);
}
});
TextView denyBtn= (TextView) dialogView.findViewById(R.id.dialog_location_deny_button);
denyBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
locationDialog.dismiss();
}
});
locationDialog.setView(dialogView);
locationDialog.show();
}else{
ActivityCompat.requestPermissions(getActivity(), PERMISSIONS_LOCATION,
LOCATION_PERMISSIONS_REQUEST);
}
}
//位置がオンまたはオフの場合、それに応じてトーストが表示されます。