我想在用户接受我的位置请求
后启动代码 @TargetApi(23)
public void checkLocationPermission() {
if (getContext().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_LOCATION);
}
Toast.makeText(getContext(), "We need permissions for check available event", Toast.LENGTH_SHORT).show();
}
@OnClick(R.id.btn_refresh_events)
public void onRefreshClick() {
checkLocationPermission();
refreshingEvents.setVisibility(View.GONE);
mPresenter.refresh();
}
如您所见,我有检查权限的方法。但现在我的代码就像按时完成所有操作一样。因此,当用户接受请求时,他必须下次单击刷新事件按钮。
我的问题是如何制作标志
首先我checkBocationPermission(),如果用户接受我的代码的权限:
refreshingEvents.setVisibility(View.GONE);
mPresenter.refresh();
在此之后运行。
请帮忙,祝你有愉快的一天! :)
答案 0 :(得分:0)
试试这个:
@TargetApi(23) public boolean checkLocationPermission() {
boolean permission granted = false;
if (getContext().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_LOCATION);
if(getContext().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
granted = true;
}
}
else{
granted = true;
}
Toast.makeText(getContext(), "We need permissions for check available event", Toast.LENGTH_SHORT).show();
return granted;
}
或
@TargetApi(23) public boolean checkLocationPermission() {
while (getContext().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_LOCATION);
Toast.makeText(getContext(), "We need permissions for check available event", Toast.LENGTH_SHORT).show();
}
return true;
}
当你想让程序保持循环,直到授予权限为止(虽然我不建议这样做)。 那你需要
@OnClick(R.id.btn_refresh_events)
public void onRefreshClick() {
boolean isPermissionGranted = checkLocationPermission();
if(isPermissionGranted){
refreshingEvents.setVisibility(View.GONE);
mPresenter.refresh();
}
}
虽然可能有更优雅的方式。