Introduction to Android Permission on Runtime for Marshmallow and Above -
Prior to the M release, the Android permissions model has been an all-or-nothing decision for users at install time. This meant that if a user wanted to use an app, they first had to accept every permission included in the application or choose to not install it at all. This led to many developers losing out on app installs, a trust disconnect between users and developers, and other privacy concerns.
Under the new permissions model, users will be able to approve permissions at runtime as they are needed and can deny those permissions at any time. In this article, you will learn how this change in handling permissions will affect you as a developer and how your users will experience your applications.
It should be noted that this article was written before the official release of Android M, so some information may change with the official release.
1. What Requires Permissions?
While Android M still requires permissions to be declared in AndroidManifest.xml, users will now be required to approve or deny the use of that permission at runtime. All Permissions are divided into two types of permissions, One is Dangerous and second one is normal.
List of Dangerous Permissions which is going to be permitted by user -
2. How to Ask for Permission?
When you need to use a feature that requires a permission, there's a general flow of events that will happen. You first need to see if that permission has already been approved by your user.
If the user has not approved the permission, you can present them with a permission request dialog. The first time you present this to the user, they will have to either deny or approve the permission.
However, if they have previously denied the permission and are asked again, they will have the option to opt out of ever being asked for that permission again.
You can check if a permission has been previously granted by calling
checkSelfPermission
before using a feature that will require that permission. This method returns an int
value based on wether that permission is granted or not.
If it is equal to
PackageManager.PERMISSION_GRANTED
, then you can continue as expected. However, if that permission has not been previously granted, you can request it with requestPermissions
, passing in an array of permission strings and a custom int
request code for keeping track of your app's logic flow.
And most important, First check version of android which is being used by user afterwards use following code -
Your Entry Class Should have this code -
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
int hasLocationPermission = checkSelfPermission( Manifest.permission.ACCESS_FINE_LOCATION );
if( hasLocationPermission != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_CONTACTS,Manifest.permission.ACCESS_COARSE_LOCATION},1);
}
}
This also -
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1)
{
Toast.makeText(this, "Enjoy", Toast.LENGTH_SHORT).show();
}
}
Manifest -
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
3. Legacy Apps on Android M
While apps that are built targeting Android M are required to implement the new permissions dialogs and methods, apps built for previous versions of Android will still present users with a list of permissions at install time. Although permissions are accepted before users can use your app, they can still be revoked at any time.
Since the infrastructure for handling revoked permissions will not be available in applications targeting less than Android M, any features that would have required permissions will return
null
, 0
, or an empty value when permissions are not granted. This can lead to unexpected behavior in apps, so it is recommended that developers prepare to upgrade their apps to support the new Android M permission model as soon as possible.
If you like our Tutorial, Please, Like our Fb Page :
No comments:
Post a Comment