As we know that with Android M, Google launched a new feature of Requesting Permission at Run Time i.e User don’t need to accept for all the permissions you are asking for, while installing the app. That’s why for all the m devices your app has to ask for permission during run time.
So how will we do that. Here we will discuss how to do that. You just need to place a check where you want that particular permission. For-example lets say my app has a download feature so I will need
1 |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> |
then just add the below code where you are providing the downloading functionality.
1 2 3 4 5 6 7 8 9 10 |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { ... // Your code for download file } else { ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); } } else{ // Your code for download file } |
The marked line will be responsible for asking user for Grating Permission if Permission is not given. Once a permission is granted it will not be asked for again. And with this you are ready to go!! Happy Coding!!