Using Permissions
A basic Android application has no permissions associated with it by default, meaning it cannot do anything that would adversely impact the user experience or any data on the device. To make use of protected features of the device, you must include one or more
<uses-permission>
tags in your app
manifest.For example, an application that needs to monitor incoming SMS messages would specify:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.app.myapp" > <uses-permission android:name="android.permission.RECEIVE_SMS" /> ...</manifest>
Permission Levels
For more information about the different protection levels for permissions, see Normal and Dangerous Permissions.- If the device is running Android 6.0 (API level 23) or higher,
and the app's
targetSdkVersion
is 23 or higher, the app requests permissions from the user at run-time. The user can revoke the permissions at any time, so the app needs to check whether it has the permissions every time it runs. For more information about requesting permissions in your app, see the Working with System Permissions training guide. - If the device is running Android 5.1 (API level 22) or lower, or
the app's
targetSdkVersion
is 22 or lower, the system asks the user to grant the permissions when the user installs the app. If you add a new permission to an updated version of the app, the system asks the user to grant that permission when the user updates the app. Once the user installs the app, the only way they can revoke the permission is by uninstalling the app.
SecurityException
being thrown back to the application.Normal and Dangerous Permissions
System permissions are divided into several protection levels. The two most important protection levels to know about are normal and dangerous permissions:
- Normal permissions cover areas where your app needs to access data or resources outside the app's sandbox, but where there's very little risk to the user's privacy or the operation of other apps. For example, permission to set the time zone is a normal permission. If an app declares that it needs a normal permission, the system automatically grants the permission to the app. For a full listing of the current normal permissions, see Normal permissions.
- Dangerous permissions cover areas where the app wants data or resources that involve the user's private information, or could potentially affect the user's stored data or the operation of other apps. For example, the ability to read the user's contacts is a dangerous permission. If an app declares that it needs a dangerous permission, the user has to explicitly grant the permission to the app.
Permission groups
All dangerous Android system permissions belong to permission groups. If the device is running Android 6.0 (API level 23) and the app'stargetSdkVersion
is 23 or higher, the following system
behavior applies when your app requests a dangerous permission:
- If an app requests a dangerous permission listed in its manifest, and the app
does not currently have any permissions in the permission group, the system
shows a dialog box to the user describing the permission group that the app
wants access to. The dialog box does not describe the specific permission
within that group. For example, if an app requests the
READ_CONTACTS
permission, the system dialog box just says the app needs access to the device's contacts. If the user grants approval, the system gives the app just the permission it requested. - If an app requests a dangerous permission listed in its manifest, and the app
already has another dangerous permission in the same permission group, the
system immediately grants the permission without any interaction with the
user. For example, if an app had previously requested and been granted the
READ_CONTACTS
permission, and it then requestsWRITE_CONTACTS
, the system immediately grants that permission.
Any permission can belong to a permission group, including normal permissions
and permissions defined by your app.
However, a permission's group only affects the user experience if the
permission is dangerous. You can ignore the permission group for normal
permissions.
Permission Group | Permissions |
---|---|
CALENDAR |
|
CAMERA |
|
CONTACTS |
|
LOCATION |
|
MICROPHONE |
|
PHONE |
|
SENSORS |
|
SMS |
|
STORAGE
|
|
Example of an permission dialog for WRITE_EXTERNAL_STORAGE:
if (ContextCompat.checkSelfPermission(con, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
if (ActivityCompat.shouldShowRequestPermissionRationale(con, Manifest.permission.WRITE_EXTERNAL_STORAGE))
{
final AlertDialog.Builder builder = new AlertDialog.Builder(con);
builder.setMessage("Apps needs few permissions.")
.setCancelable(false)
.setPositiveButton("Allow", new DialogInterface.OnClickListener()
{
public void onClick(final DialogInterface dialog, final int id)
{
ActivityCompat.requestPermissions(con,
new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE}, 11);
}
}).setNegativeButton("Reject", new DialogInterface.OnClickListener()
{
public void onClick(final DialogInterface dialog, final int id)
{
dialog.cancel();
finish();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
else
{
ActivityCompat.requestPermissions(con,
new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE}, 11);
}
}
You can add permissions in same conditions and it will open permissions request dialog (android default).
0 comments:
Post a Comment