How to check permission in fragment

前端 未结 9 1931
执念已碎
执念已碎 2020-12-01 03:49

I want to check a permission inside a fragment.

my code:

        // Here, thisActivity is the current activity
        if (ContextCompat.checkSelfPe         


        
相关标签:
9条回答
  • 2020-12-01 04:27

    If you closed your permission of app from settings , you can not open your permission from code or your android version lower than Marshmallow.

    You can check this documentation https://developer.android.com/training/permissions/requesting.html And this is a example https://www.learn2crack.com/2015/10/android-marshmallow-permissions.html

    0 讨论(0)
  • 2020-12-01 04:32

    I have done following to check a permission inside a fragment.

    if (ActivityCompat.checkSelfPermission(getContext(),
                android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(getContext(),
                        android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
             requestPermissions(getActivity(),
                    new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION,
                            android.Manifest.permission.ACCESS_FINE_LOCATION},
                    REQUEST_LOCATION);
        } else {
            Log.e("DB", "PERMISSION GRANTED");
        }
    
    0 讨论(0)
  • 2020-12-01 04:36

    Using Kotlin, you call requestPermissions(arrayOf(Manifest.permission.THE_PERMISSION_CODE_YOU_WANT), PERMISSION_REQUEST_CODE) and add the following override to your fragment

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out kotlin.String>, grantResults: IntArray): Unit {
    
    }
    
    0 讨论(0)
  • 2020-12-01 04:36

    What worked for me was calling the onRequestPermissionsResult method in the activity inside which fragment is implemented rather than calling it in fragment itself. Inside onCreateView method in fragment:

        Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int permissionCheck = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);
    
                if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_MEDIA);
                }else{
                    //Do your work
                    fetchMethod();
                }
    
            }
    });
    

    In the Activity which helps to implement fragment, outside of onCreate method:

        @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_READ_MEDIA:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    fetchMethod();
    
                }else{
                    Toast.makeText(getApplicationContext(), "Permission not granted!", Toast.LENGTH_SHORT).show();
                }
                break;
    
            default:
                break;
        }
    }
    
    0 讨论(0)
  • 2020-12-01 04:38

    I was getting tripped up using checkSelfPermission() in a Fragment and wondering what would be the best approach for Context being null (Kotlin specific)... should I use !! or something else?

    I went with something else based on code I found in iosched. Have a look at the sample below, and remember, before the Fragment is attached to an Activity, the Context will be null.

    private fun fineLocationPermissionApproved(): Boolean {
    
        val context = context ?: return false
    
        return PackageManager.PERMISSION_GRANTED == checkSelfPermission(
            context,
            Manifest.permission.ACCESS_FINE_LOCATION
        )
    }
    
    0 讨论(0)
  • 2020-12-01 04:41

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

    For Activity :

    ActivityCompat.requestPermissions(this, permissionsList, REQUEST_CODE);
    

    For Fragment :

    requestPermissions(permissionsList, REQUEST_CODE);
    
    0 讨论(0)
提交回复
热议问题