在我的android项目中,我使用以下代码检查并请求运行时权限:
public class AgentsFragmentMapTab extends RootFragment implements ActivityCompat.OnRequestPermissionsResultCallback
{
//..
private boolean checkAndRequestPermissions() {
int internet = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.INTERNET);
int loc = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION);
int loc2 = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION);
List<String> listPermissionsNeeded = new ArrayList<>();
if (internet != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.INTERNET);
}
if (loc != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.ACCESS_COARSE_LOCATION);
}
if (loc2 != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (BuildConfig.DEBUG)
Log.d(TAG, "checkAndRequestPermissions_listPermissionsNeeded(" + listPermissionsNeeded.size() + ")");
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(getActivity(), listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), LOCATION_REQ_CODE);
return false;
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == LOCATION_REQ_CODE) {
} else {
}
}
}
此代码完美显示了权限对话框。但是,当我单击允许或拒绝按钮时,方法onRequestPermissionsResult()
不会被调用。为什么不调用它以及如何实现呢?
答案 0 :(得分:0)
如父活动中的覆盖onRequestPermissionsResult
的注释中所述
请确保从此活动的super.onRequestPermissionsResult
呼叫onRequestPermissionsResult
然后它将发送到覆盖onRequestPermissionsResult
已编辑:
这就是我所做的
我叫requestPermissions(StringOfPermissions,YourRequestCode);
片段内
这是我用来在活动中添加片段的代码,很简单
getSupportFragmentManager()
.beginTransaction()
.add(R.id.container, BlankFragment.newInstance("",""))
.disallowAddToBackStack()
.commit();
我覆盖了onRequestPermissionsResult():
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
活动和片段内
当我从片段中请求时,它进入活动,然后进入片段
答案 1 :(得分:0)
更改
ActivityCompat.requestPermissions(getActivity(), listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), LOCATION_REQ_CODE);
收件人:
requestPermissions(listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_CODE);
还有ContextCompat
至ActivityCompat
和getActivity()
至getContext()
,例如:
int loc2 = ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION, LOCATION_REQ_CODE);
答案 2 :(得分:0)
首先,在父活动中执行此操作。
...
Fragment theFragment =
getSupportFragmentManager().findFragmentById(R.id.your_fragment_name);
...
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case LOCATION_REQ_CODE: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if(theFragment instanceof AgentsFragmentMapTab)
((AgentsFragmentMapTab)theFragment).passPermissionResult(true);
} else {
if(theFragment instanceof AgentsFragmentMapTab)
((AgentsFragmentMapTab)theFragment).passPermissionResult(false);
}
return;
}
}
}
然后在您的片段中,添加一个公共方法,如下所示:
public void passPermissionResult(boolean allowed) {
if(allowed) {
// ALLOW clicked
}
else {
// DENY clicked
}
}