Android:在请求权限方面出现了问题。代码如下:

huangapple go评论108阅读模式
英文:

Android: Issues with my asking for permissions. Code inside

问题

以下是您提供的代码的翻译部分:

  1. public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  2. Button getPermissions = (Button) getView().findViewById(R.id.get_permissions);
  3. TextView permissionsText = (TextView) getView().findViewById(R.id.permissions_text);
  4. getPermissions.setOnClickListener(new View.OnClickListener() {
  5. @Override
  6. public void onClick(View view) {
  7. if (checkPermission()) {
  8. permissionsText.setText("已授权");
  9. permissionsText.setTextColor(Color.GREEN);
  10. } else {
  11. requestPermission();
  12. }
  13. }
  14. });
  15. }
  16. private void requestPermission() {
  17. ActivityCompat.requestPermissions(getActivity(), new String[]
  18. {
  19. ACCESS_FINE_LOCATION,
  20. ACCESS_COARSE_LOCATION,
  21. ACCESS_BACKGROUND_LOCATION,
  22. CALL_PHONE,
  23. INTERNET,
  24. READ_EXTERNAL_STORAGE,
  25. VIBRATE,
  26. USE_FULL_SCREEN_INTENT
  27. }, RequestPermissionCode);
  28. }
  29. @Override
  30. public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
  31. switch (requestCode) {
  32. case RequestPermissionCode:
  33. if (grantResults.length > 0) {
  34. boolean fineLocPermission = grantResults[0] == PackageManager.PERMISSION_GRANTED;
  35. boolean coarseLocPermission = grantResults[1] == PackageManager.PERMISSION_GRANTED;
  36. boolean backgroundLocPermission = grantResults[2] == PackageManager.PERMISSION_GRANTED;
  37. boolean phonePermission = grantResults[3] == PackageManager.PERMISSION_GRANTED;
  38. boolean internetPermission = grantResults[4] == PackageManager.PERMISSION_GRANTED;
  39. boolean readExternalStoragePermission = grantResults[5] == PackageManager.PERMISSION_GRANTED;
  40. boolean vibratePermission = grantResults[6] == PackageManager.PERMISSION_GRANTED;
  41. boolean fullScreenIntentPermission = grantResults[7] == PackageManager.PERMISSION_GRANTED;
  42. if (fineLocPermission && coarseLocPermission && backgroundLocPermission &&
  43. phonePermission && internetPermission && readExternalStoragePermission &&
  44. vibratePermission && fullScreenIntentPermission)
  45. {
  46. Toast.makeText(getActivity(), "已授予权限", Toast.LENGTH_LONG).show();
  47. }
  48. else {
  49. Toast.makeText(getActivity(), "权限已拒绝", Toast.LENGTH_LONG).show();
  50. }
  51. }
  52. break;
  53. }
  54. }
  55. public boolean checkPermission() {
  56. int FirstPermissionResult = ContextCompat.checkSelfPermission(getContext(), ACCESS_FINE_LOCATION);
  57. int SecondPermissionResult = ContextCompat.checkSelfPermission(getContext(), ACCESS_COARSE_LOCATION);
  58. int ThirdPermissionResult = ContextCompat.checkSelfPermission(getContext(), ACCESS_BACKGROUND_LOCATION);
  59. int FourthPermissionResult = ContextCompat.checkSelfPermission(getContext(), CALL_PHONE);
  60. int FifthPermissionResult = ContextCompat.checkSelfPermission(getContext(), INTERNET);
  61. int SixthPermissionResult = ContextCompat.checkSelfPermission(getContext(), READ_EXTERNAL_STORAGE);
  62. int SeventhPermissionResult = ContextCompat.checkSelfPermission(getContext(), VIBRATE);
  63. int EighthPermissionResult = ContextCompat.checkSelfPermission(getContext(), USE_FULL_SCREEN_INTENT);
  64. return FirstPermissionResult == PackageManager.PERMISSION_GRANTED &&
  65. SecondPermissionResult == PackageManager.PERMISSION_GRANTED &&
  66. ThirdPermissionResult == PackageManager.PERMISSION_GRANTED &&
  67. FourthPermissionResult == PackageManager.PERMISSION_GRANTED &&
  68. FifthPermissionResult == PackageManager.PERMISSION_GRANTED &&
  69. SixthPermissionResult == PackageManager.PERMISSION_GRANTED &&
  70. SeventhPermissionResult == PackageManager.PERMISSION_GRANTED &&
  71. EighthPermissionResult == PackageManager.PERMISSION_GRANTED;
  72. }

请注意,以上是代码的翻译,其中的注释、变量名等未包含在内。

英文:

I am trying to give the user an option to grant permission in my code as per Google best practices. The code below is what I have tried. The first time I ran it on the emulator it asked for a permission. Nothing else was done. No toast showed up, no green "Granted" text, etc. Logcat is silent on the issue. Does anyone see what have I missed? (Note: This code is inside a fragment).

  1. public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  2. Button getPermissions = (Button)getView().findViewById(R.id.get_permissions);
  3. TextView permissionsText = (TextView)getView().findViewById(R.id.permissions_text);
  4. getPermissions.setOnClickListener(new View.OnClickListener() {
  5. @Override
  6. public void onClick(View view) {
  7. if(checkPermission()){
  8. permissionsText.setText("Granted");
  9. permissionsText.setTextColor(Color.GREEN);
  10. }
  11. else {
  12. requestPermission();
  13. }
  14. }
  15. });
  16. }
  17. private void requestPermission() {
  18. ActivityCompat.requestPermissions(getActivity(), new String[]
  19. {
  20. ACCESS_FINE_LOCATION,
  21. ACCESS_COARSE_LOCATION,
  22. ACCESS_BACKGROUND_LOCATION,
  23. CALL_PHONE,
  24. INTERNET,
  25. READ_EXTERNAL_STORAGE,
  26. VIBRATE,
  27. USE_FULL_SCREEN_INTENT
  28. }, RequestPermissionCode);
  29. }
  30. @Override
  31. public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
  32. switch (requestCode) {
  33. case RequestPermissionCode:
  34. if (grantResults.length > 0) {
  35. boolean fineLocPermission = grantResults[0] == PackageManager.PERMISSION_GRANTED;
  36. boolean coarseLocPermission = grantResults[1] == PackageManager.PERMISSION_GRANTED;
  37. boolean backgroundLocPermission = grantResults[2] == PackageManager.PERMISSION_GRANTED;
  38. boolean phonePermission = grantResults[3] == PackageManager.PERMISSION_GRANTED;
  39. boolean internetPermission = grantResults[4] == PackageManager.PERMISSION_GRANTED;
  40. boolean readExternalStoragePermission = grantResults[5] == PackageManager.PERMISSION_GRANTED;
  41. boolean vibratePermission = grantResults[6] == PackageManager.PERMISSION_GRANTED;
  42. boolean fullScreenIntentPermission = grantResults[7] == PackageManager.PERMISSION_GRANTED;
  43. if (fineLocPermission && coarseLocPermission && backgroundLocPermission && phonePermission && internetPermission && readExternalStoragePermission && vibratePermission && fullScreenIntentPermission)
  44. {
  45. Toast.makeText(getActivity(),"Permission Granted",Toast.LENGTH_LONG).show();
  46. }
  47. else {
  48. Toast.makeText(getActivity(),"Permission Denied",Toast.LENGTH_LONG).show();
  49. }
  50. }
  51. break;
  52. }
  53. }
  54. public boolean checkPermission() {
  55. int FirstPermissionResult = ContextCompat.checkSelfPermission(getContext(), ACCESS_FINE_LOCATION);
  56. int SecondPermissionResult = ContextCompat.checkSelfPermission(getContext(), ACCESS_COARSE_LOCATION);
  57. int ThirdPermissionResult = ContextCompat.checkSelfPermission(getContext(), ACCESS_BACKGROUND_LOCATION);
  58. int FourthPermissionResult = ContextCompat.checkSelfPermission(getContext(), CALL_PHONE);
  59. int FifthPermissionResult = ContextCompat.checkSelfPermission(getContext(), INTERNET);
  60. int SixthPermissionResult = ContextCompat.checkSelfPermission(getContext(), READ_EXTERNAL_STORAGE);
  61. int SeventhPermissionResult = ContextCompat.checkSelfPermission(getContext(), VIBRATE);
  62. int EigthPermissionResult = ContextCompat.checkSelfPermission(getContext(), USE_FULL_SCREEN_INTENT);
  63. return FirstPermissionResult == PackageManager.PERMISSION_GRANTED &&
  64. SecondPermissionResult == PackageManager.PERMISSION_GRANTED &&
  65. ThirdPermissionResult == PackageManager.PERMISSION_GRANTED &&
  66. FourthPermissionResult == PackageManager.PERMISSION_GRANTED &&
  67. FifthPermissionResult == PackageManager.PERMISSION_GRANTED &&
  68. SixthPermissionResult == PackageManager.PERMISSION_GRANTED &&
  69. SeventhPermissionResult == PackageManager.PERMISSION_GRANTED &&
  70. EigthPermissionResult == PackageManager.PERMISSION_GRANTED;
  71. }

答案1

得分: 1

在Fragment中使用requestPermissions(arrayOfPermissions, REQ_CODE)代替ActivityCompat.requestPermissions()

一个Fragment有一个名为requestPermissions()的方法,我不是指你在代码中命名的方法。

然后,在Fragment中会正确地调用onRequestPermissionsResult

示例代码:

  1. @Override
  2. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  3. super.onViewCreated(view, savedInstanceState);
  4. requestPermissions(new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, REQ_CODE_PERMISSION);
  5. }
英文:

In a Fragment use requestPermissions(arrayOfPermissions, REQ_CODE) instead of ActivityCompat.requestPermissions():

A Fragment has a method named requestPermissions() and I don't mean the method you named it in your code.

onRequestPermissionsResult is then invoked correctly in the Fragment.

Sample Code:

  1. @Override
  2. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  3. super.onViewCreated(view, savedInstanceState);
  4. requestPermissions(new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, REQ_CODE_PERMISSION);
  5. }

huangapple
  • 本文由 发表于 2020年4月9日 02:49:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/61107908.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定