Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check permission in fragment

I want to check a permission inside a fragment.

my code:

        // Here, thisActivity is the current activity         if (ContextCompat.checkSelfPermission(getActivity(),                 Manifest.permission.ACCESS_FINE_LOCATION)                 != PackageManager.PERMISSION_GRANTED) {               // Should we show an explanation?             if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),                     android.Manifest.permission.ACCESS_FINE_LOCATION)) {                  // Show an explanation to the user *asynchronously* -- don't block                 // this thread waiting for the user's response! After the user                 // sees the explanation, try again to request the permission.              } else {                  // No explanation needed, we can request the permission.                  ActivityCompat.requestPermissions(getActivity(),                         new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},                         1);                    // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an                 // app-defined int constant. The callback method gets the                 // result of the request.             }         } 

but onRequestPermissionsResult not called after allow or deny.

@Override public void onRequestPermissionsResult(int requestCode,                                        String permissions[], int[] grantResults) {     switch (requestCode) {         case 1: {             Log.e("test","0");             // If request is cancelled, the result arrays are empty.             if (grantResults.length > 0                     && grantResults[0] == PackageManager.PERMISSION_GRANTED) {                  // permission was granted, yay! Do the                 // contacts-related task you need to do.                 //yes                  Log.e("test","1");                  Intent intent = new Intent(getActivity(), MapsActivity.class);                 intent.putExtra("latitude", 35.694828);                 intent.putExtra("longitude", 51.378129);                 startActivity(intent);              } else {                 utilityFunctions.showSweetAlertWarning(getActivity(),r.getString(R.string.str_warning_title_empty),                         r.getString(R.string.str_you_must_allow_this_permission_toast),                         r.getString(R.string.str_warning_btn_login));                  Log.e("test","2");             }             return;         }          // other 'case' lines to check for other         // permissions this app might request     } } 
like image 481
S.M_Emamian Avatar asked Nov 23 '16 09:11

S.M_Emamian


People also ask

How do I check permissions on Android?

To check if the user has already granted your app a particular permission, pass that permission into the ContextCompat. checkSelfPermission() method. This method returns either PERMISSION_GRANTED or PERMISSION_DENIED , depending on whether your app has the permission.

How do I get runtime permission on Android?

checkSelfPermission(String perm); It returns an integer value of PERMISSION_GRANTED or PERMISSION_DENIED. Note: If a user declines a permission that is critical in the app, then shouldShowRequestPermissionRationale(String permission); is used to describe the user the need for the permission.


2 Answers

This is how I did, it works for me. Thanks!

For Activity :

ActivityCompat.requestPermissions(this, permissionsList, REQUEST_CODE); 

For Fragment :

requestPermissions(permissionsList, REQUEST_CODE); 
like image 126
ThetNaing Mizo Avatar answered Sep 25 '22 23:09

ThetNaing Mizo


Fragment has requestPermissions() and onRequestPermissionsResult() methods, use it.

But checkSelfPermission() is from ActivityCompat (not require Activity, only Context).

if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {     requestPermissions( //Method of Fragment         new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},          REQUEST_PERMISSIONS_CODE_WRITE_STORAGE     ); } else {     downloadImage(); }  @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {     if (requestCode == REQUEST_PERMISSIONS_CODE_WRITE_STORAGE) {         if (permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)                 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {             proceedWithSdCard();         }     } } 
like image 36
Evgeny Nozdrev Avatar answered Sep 23 '22 23:09

Evgeny Nozdrev