Android onRequestPermissionsResult 不适用于我的片段
Posted
技术标签:
【中文标题】Android onRequestPermissionsResult 不适用于我的片段【英文标题】:Android onRequestPermissionsResult not working on my fragment 【发布时间】:2017-10-01 00:22:15 【问题描述】:在我的活动页面之一的片段中,我想获得用户的许可并在授予许可后执行一些操作,但 onRequestPermissionsResult
不起作用,并且在授予许可后我没有看到任何日志
public class FragmentAboutUs extends Fragment implements ActivityCompat.OnRequestPermissionsResultCallback
private Activity activity;
private Context context;
...
private int WRITE_EXTERNAL_STORAGE = 1;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_about_us, container, false);
activity = getActivity();
context = getActivity().getBaseContext();
...
aboutUs = SQLite.select().from(AboutUs.class).querySingle();
if (aboutUs == null)
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE))
ActivityCompat.requestPermissions(activity, new String[]Manifest.permission.WRITE_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE);
else
ActivityCompat.requestPermissions(activity, new String[]Manifest.permission.WRITE_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE);
return view;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
if (requestCode == WRITE_EXTERNAL_STORAGE)
Log.e("permission granted"," OK");
else
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
【问题讨论】:
你的 onRequestPermissionsResult 在哪里 【参考方案1】:使用:
requestPermissions(permissions, PERMISSIONS_CODE);
取而代之的是你的:
ActivityCompat.requestPermissions()
您可以在此处阅读更多内容: Answer
【讨论】:
【参考方案2】:删除这个:
implements ActivityCompat.OnRequestPermissionsResultCallback
Fragment 可以自己处理 OnRequestPermissionsResultCallback
的 Runtime 权限:
像这样使用它:
final int MyVersion = Build.VERSION.SDK_INT;
if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1)
if (!checkIfAlreadyhavePermission())
requestPermissions(new String[]Manifest.permission.WRITE_EXTERNAL_STORAGE , WRITE_EXTERNAL_STORAGE); //request permission
else
//call your method permision already granted
else
//call your method permission not needed for this version
private boolean checkIfAlreadyhavePermission()
int result = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
return result == PackageManager.PERMISSION_GRANTED;
现在您可以在Fragment
内的public void onRequestPermissionsResult()
中获取权限结果
使用:
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
switch (requestCode)
case WRITE_EXTERNAL_STORAGE:
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED)
Toast.makeText(getActivity(), "permission granted", Toast.LENGTH_LONG).show();
//call your action
else
Toast.makeText(getActivity(), "permission denied", Toast.LENGTH_LONG).show();
break;
【讨论】:
那么在删除implements ActivityCompat.OnRequestPermissionsResultCallback
后授予权限后如何执行自定义操作?
您可以在onRequestPermissionsResult()
中调用您的操作...授予权限后--我已对其进行了编辑
requestPermissions(...)
不检查应用是否有权限?看来我必须使用checkIfAlreadyhavePermission
才能获得许可?以上是关于Android onRequestPermissionsResult 不适用于我的片段的主要内容,如果未能解决你的问题,请参考以下文章
Android 逆向Android 权限 ( Android 逆向中使用的 android.permission 权限 | Android 系统中的 Linux 用户权限 )