1page.title=Permissions 2page.tags=previewresources, androidm 3page.keywords=permissions, runtime, preview 4page.image=images/permissions_check.png 5@jd:body 6 7 8<div id="qv-wrapper"> 9 <div id="qv"> 10 <h2>Quickview</h2> 11 <ul> 12 <li>If your app targets the M Preview SDK, it prompts users to grant 13 permissions at runtime, instead of install time.</li> 14 <li>Users can revoke permissions at any time from the app Settings 15 screen.</li> 16 <li>Your app needs to check that it has the permissions it needs every 17 time it runs.</li> 18 </ul> 19 20 <h2>In this document</h2> 21 <ol> 22 <li><a href="#overview">Overview</a></li> 23 <li><a href="#coding">Coding for Runtime Permissions</a></li> 24 <li><a href="#testing">Testing Runtime Permissions</a></li> 25 <li><a href="#best-practices">Best Practices and Usage Notes</a></li> 26 </ol> 27 28<!-- 29 <h2>Related Samples</h2> 30 <ol> 31 <li></li> 32 </ol> 33--> 34 35<!-- 36 <h2>See also</h2> 37 <ol> 38 <li> 39 </li> 40 </ol> 41--> 42 43 </div> <!-- qv --> 44</div> <!-- qv-wrapper --> 45 46<!-- video box --> 47<a class="notice-developers-video" 48 href="https://www.youtube.com/watch?v=f17qe9vZ8RM"> 49<div> 50 <h3>Video</h3> 51 <p>Google I/O 2015—Android M Permissions: Best Practices for 52 Developers</p> 53</div> 54</a> 55 56<p> 57 The M Developer Preview introduces a new app permissions model which 58 streamlines the process for users to install and upgrade apps. If an app 59 running on the M Preview supports the new permissions model, the user does not have to 60 grant any permissions when they install or upgrade the app. Instead, the app 61 requests permissions as it needs them, and the system shows a dialog to the 62 user asking for the permission. 63</p> 64 65<p> 66 If an app supports the new permissions model, it can still be installed and 67 run on devices running older versions of Android, using the old permissions 68 model on those devices. 69</p> 70 71<h2 id="overview"> 72 Overview 73</h2> 74 75<p> 76 With the M Developer Preview, the platform introduces a new app permissions 77 model. Here's a summary of the key components of this new model: 78</p> 79 80<ul> 81 <li> 82 <strong>Declaring Permissions:</strong> The app declares all the 83 permissions it needs in the manifest, as in earlier Android platforms. 84 </li> 85 86 <li> 87 <strong>Permission Groups:</strong> Permissions are divided into 88 <em>permission groups</em>, based on their functionality. For example, the 89 <code>CONTACTS</code> permission group contains permissions to read and 90 write the user's contacts and profile information. 91 </li> 92 93 <li> 94 <p><strong>Limited Permissions Granted at Install Time:</strong> When the 95 user installs or updates the app, the system grants the app all 96 permissions listed in the manifest that fall under {@link 97 android.content.pm.PermissionInfo#PROTECTION_NORMAL PROTECTION_NORMAL}. 98 For example, alarm clock and internet permissions fall under {@link 99 android.content.pm.PermissionInfo#PROTECTION_NORMAL PROTECTION_NORMAL}, so 100 they are automatically granted at install time. For more information about 101 how normal permissions are handled, see <a href="#normal">Normal 102 Permissions</a>. 103 </p> 104 105 <p>The system may also grant the app signature permissions, as 106 described in <a href="#system-apps">System components and signature 107 permissions</a>. The user is <em>not</em> prompted to grant any permissions 108 at install time.</p> 109 </li> 110 111 <li> 112 <strong>User Grants Permissions at Run-Time:</strong> When the app requests 113 a permission, the system shows a dialog to the user, then calls the app's 114 callback function to notify it whether the user granted the permission. 115 </li> 116 117</ul> 118 119<p> 120 This permission model changes the way your app behaves for features that 121 require permissions. Here's a summary of the development practices you should 122 follow to adjust to this model: 123</p> 124 125<ul> 126 127 <li> 128 <strong>Always Check for Permissions:</strong> When the app needs to 129 perform any action that requires a permission, it should first check 130 whether it has that permission already. If it does not, it requests to be 131 granted that permission. You do not need to check for permissions that 132 fall under {@link 133 android.content.pm.PermissionInfo#PROTECTION_NORMAL PROTECTION_NORMAL}. 134 </li> 135 136 <li> 137 <strong>Handle Lack of Permissions Gracefully:</strong> If the app is not 138 granted an appropriate permission, it should handle the failure cleanly. 139 For example, if the permission is just needed for an added feature, the app 140 can disable that feature. If the permission is essential for the app to 141 function, the app might disable all its functionality and inform the user 142 that they need to grant that permission. 143 </li> 144 145 <div class="figure" style="width:220px" id="fig-perms-screen"> 146 <img src="images/app-permissions-screen_2x.png" 147 srcset="images/app-permissions-screen.png 1x, images/app-permissions-screen_2x.png 2x" 148 alt="" width="220"> 149 <p class="img-caption"> 150 <strong>Figure 1.</strong> Permission screen in the app's Settings. 151 </p> 152 </div> 153 154 <li> 155 <strong>Permissions are Revocable:</strong> Users can revoke an app's 156 permissions at any time. If a user turns off an app's permissions, the app 157 is <em>not</em> notified. Once again, your app should verify that it has 158 needed permissions before performing any restricted actions. 159 </li> 160</ul> 161 162<p class="note"> 163 <strong>Note:</strong> If an app targets the M Developer Preview, it 164 <em>must</em> use the new permissions model. 165</p> 166 167<p> 168 As of the launch of the M Developer Preview, not all Google apps fully 169 implement the new permissions model. Google is updating these apps over 170 the course of the M Developer Preview to properly respect Permissions toggle 171 settings. 172</p> 173 174<p class="note"> 175 <strong>Note:</strong> If your app has its own API surface, do not proxy 176 permissions without first ensuring the caller has the requisite permissions 177 to access that data. 178</p> 179 180<h3 id="perm-groups">Permission groups</h3> 181 182<p> 183 Related permissions are divided into <em>permission groups</em> to allow 184 users to grant related permissions to an app in a single action. The user 185 only has to grant permission once per app for each permission group. If the 186 app subsequently requests a permission from the same permission group, the 187 system automatically grants the permission without any action from the user. 188 The system calls your app's {@link 189 android.app.Activity#onRequestPermissionsResult onRequestPermissionsResult()} 190 method just as if the user had granted permission through the dialog box. 191</p> 192 193<p> 194 For example, suppose an app lists in its manifest that it needs the 195 <code>SEND_SMS</code> and <code>RECEIVE_SMS</code> permissions, which both 196 belong to <code>android.permission-group.SMS</code>. When the app needs to 197 send a message, it requests the <code>SEND_SMS</code> permission. The system 198 shows the user a dialog box asking if the app can have access to SMS. If the 199 user agrees, the system grants the app the <code>SEND_SMS</code> permission it 200 requested. Later, the app requests <code>RECEIVE_SMS</code>. The 201 system automatically grants this permission, since the user had already 202 approved a permission in the same permission group. 203</p> 204 205<h3 id="system-apps"> 206 System components and signature permissions 207</h3> 208 209<p> 210 Ordinarily, when the user installs an app, the system only grants the app the 211 permissions listed in the manifest that fall under 212 {@link android.content.pm.PermissionInfo#PROTECTION_NORMAL 213 PROTECTION_NORMAL}. However, under some circumstances the system grants the 214 app more permissions: 215</p> 216 217<ul> 218 <li>System components automatically receive all 219 the permissions listed in their manifests. 220 </li> 221 222 <li>If the app requests permissions in the manifest that fall under {@link 223 android.content.pm.PermissionInfo#PROTECTION_SIGNATURE PROTECTION_SIGNATURE}, 224 and the app is signed with the same certificate as the app that declared 225 those permissions, the system grants the requesting app those permissions on 226 installation. Apps cannot request signature permissions at runtime.</li> 227</ul> 228 229<h3 id="compatibility"> 230 Forwards and backwards compatibility 231</h3> 232 233<p> 234 If an app does not target the M Developer Preview, the app continues to use 235 the old permissions model even on M Preview devices. When the user installs 236 the app, the system asks the user to grant all permissions listed in the 237 app's manifest. 238</p> 239 240<p class="note"> 241 <strong>Note:</strong> On devices running the M Developer Preview, a user can 242 turn off permissions for any app (including legacy apps) from the app's 243 Settings screen. If a user turns off permissions for a legacy app, the system 244 silently disables the appropriate functionality. When the app attempts to 245 perform an operation that requires that permission, the operation will not 246 necessarily cause an exception. Instead, it might return an empty data set, 247 signal an error, or otherwise exhibit unexpected behavior. For example, if you 248 query a calendar without permission, the method returns an empty data set. 249</p> 250 251<p> 252 If you install an app using the new permissions model on a device that is not 253 running the M Preview, 254 the system treats it the same as any other app: the system asks 255 the user to grant all declared permissions at install time. 256</p> 257 258<p class="note"> 259 <strong>Note:</strong> For the preview release, you must set the minimum SDK 260 version to the M Preview SDK to compile with the preview SDK. This means you 261 will not be able to test such apps on older platforms during the developer 262 preview. 263</p> 264 265<h3 id="perms-vs-intents">Permissions versus intents</h3> 266 267<p> 268 In many cases, you can choose between two ways for your app to perform a 269 task. You can have your app ask for permission to perform the operation 270 itself. Alternatively, you can have the app use an intent to have another app 271 perform the task. 272</p> 273 274<p> 275 For example, suppose your app needs to be able to take pictures with the 276 device camera. Your app can request the 277 <code>android.permission.CAMERA</code> permission, which allows your app to 278 access the camera directly. Your app would then use the camera APIs 279 to control the camera and take a picture. This approach gives your app full 280 control over the photography process, and lets you incorporate the camera UI 281 into your app. 282</p> 283 284<p> 285 However, if you don't need such control, you can just use an {@link 286 android.provider.MediaStore#ACTION_IMAGE_CAPTURE ACTION_IMAGE_CAPTURE} intent 287 to request an image. When you start the intent, the user is prompted to 288 choose a camera app (if there isn't already a default camera app), and that 289 app takes the picture. The camera app returns the picture to your app's {@link 290 android.app.Activity#onActivityResult onActivityResult()} method. 291</p> 292 293<p> 294 Similarly, if you need to make a phone call, access the user's contacts, and 295 so on, you can do that by creating an appropriate intent, or you can request 296 the permission and access the appropriate objects directly. There are 297 advantages and disadvantages to each approach. 298</p> 299 300<p> 301 If you use permissions: 302</p> 303 304<ul> 305 <li>Your app has full control over the user experience when you perform the 306 operation. However, such broad control adds to the complexity of your task, 307 since you need to design an appropriate UI. 308 </li> 309 310 <li>The user is prompted to give permission once, the first time you perform 311 the operation. After that, your app can perform the operation without 312 requiring additional interaction from the user. However, if the user doesn't 313 grant the permission (or revokes it later on), your app becomes unable to 314 perform the operation at all. 315 </li> 316</ul> 317 318<p> 319 If you use an intent: 320</p> 321 322<ul> 323 <li>You do not have to design the UI for the operation. The app that handles 324 the intent provides the UI. However, this means you have 325 no control over the user experience. The user could be interacting with an 326 app you've never seen. 327 </li> 328 329 <li>If the user does not have a default app for the operation, the system 330 prompts the user to choose an app. If the user does not designate a default 331 handler, they may have to go 332 through an extra dialog every time they perform the operation. 333 </li> 334</ul> 335 336<h2 id="coding">Coding for Runtime Permissions</h2> 337 338<p> 339 If your app targets the new M Developer Preview, you must use the new 340 permissions model. This means that in addition to declaring your needed 341 permissions in the manifest, you must also check to see if you have the 342 permissions at run time, and request the permissions if you do not already 343 have them. 344</p> 345 346<h3 id="enabling"> 347 Enabling the new permissions model 348</h3> 349 350<p> 351 To enable the new M Developer Preview permissions model, set the app's 352 <code>targetSdkVersion</code> attribute to <code>"MNC"</code>, and 353 <code>compileSdkVersion</code> to <code>"android-MNC"</code>. Doing so 354 enables all the new permissions features. 355</p> 356 357<p> 358 For the preview release, you must set <code>minSdkVersion</code> to 359 <code>"MNC"</code> to compile with the preview SDK. 360</p> 361 362<h3 id="m-only-perm"> 363 Designating a permission for the M Preview only 364</h3> 365 366<p> 367 You can use the new <code><uses-permission-sdk-m></code> element in the app manifest 368 to indicate that a permission is only needed on the M Developer Preview. If 369 you declare a permission this way, then whenever the app is installed on an 370 older device, the system does not prompt the user or grant the 371 permission to the app. By using the <code><uses-permission-sdk-m></code> 372 element, you can add new permissions 373 to updated versions of your app without forcing users to grant permissions 374 when they install the update. 375</p> 376 377<p> 378 If the app is running on a device with the M Developer Preview, 379 <code><uses-permission-sdk-m></code> behaves the same as 380 <code><a href="{@docRoot}guide/topics/manifest/uses-permission-element.html" 381 ><uses-permission></a></code>. 382 The system does not prompt the user to grant any permissions when they install 383 the app, and the app requests permissions as they are needed. 384</p> 385 386<h3 id="prompting"> 387 Prompting for permissions 388</h3> 389 390<p> 391 If your app uses the new M Developer Preview permissions model, the user is 392 not asked to grant all permissions when the app is first launched on a device 393 running the M Preview. Instead, your app requests permissions as they are 394 needed. When your app requests a permission, the system shows a dialog to the 395 user. 396</p> 397 398<p> 399 If your app runs on a device that has SDK 22 or lower, the app uses the old 400 permissions model. When the user installs the app, they are prompted to grant 401 all the permissions your app requests in its manifest, except for those 402 permissions which are labeled with <code><uses-permission-sdk-m></code>. 403</p> 404 405<h4 id="check-platform">Check what platform the app is running on</h4> 406 407<p> 408 This permissions model is only supported on devices running the M Developer 409 Preview. Before calling any of these methods, the app should verify 410 what platform it's running on 411 by checking the value of {@link android.os.Build.VERSION#CODENAME 412 Build.VERSION.CODENAME}. If the device is running the M Developer Preview, 413 {@link android.os.Build.VERSION#CODENAME CODENAME} is <code>"MNC"</code>. 414</p> 415 416<p> 417 Alternatively, you can use the new methods introduced with revision 23 of the 418 v4 and v13 support libraries. The support library methods behave 419 appropriately whether or not the app is running on the M Developer Preview. 420 For more information, see <a href="#support-lib">Support library methods for 421 handling permissions</a>. 422</p> 423 424<h4 id="check-for-permission">Check if the app has the needed permission</h4> 425 426<p> 427 When the user tries to do something that requires a permission, the app 428 checks to see if it currently has permission to perform this operation. To do 429 this, the app calls {@link android.content.Context#checkSelfPermission 430 checkSelfPermission()}. The app should perform this check even if it knows 431 the user has already granted that permission, since the user can revoke an 432 app's permissions at any time. For example, if a user wants to use an app to 433 take a picture, the app calls 434 <code>Context.checkSelfPermission(Manifest.permission.CAMERA)</code>. 435</p> 436 437<p class="table-caption" id="permission-groups"> 438 <strong>Table 1.</strong> Permissions and permission groups.</p> 439<table> 440 <tr> 441 <th scope="col">Permission Group</th> 442 <th scope="col">Permissions</th> 443 </tr> 444 445 <tr> 446 <td><code>android.permission-group.CALENDAR</code></td> 447 <td> 448 <ul> 449 <li> 450 <code>android.permission.READ_CALENDAR</code> 451 </li> 452 </ul> 453 <ul> 454 <li> 455 <code>android.permission.WRITE_CALENDAR</code> 456 </li> 457 </ul> 458 </td> 459 </tr> 460 461 <tr> 462 <td><code>android.permission-group.CAMERA</code></td> 463 <td> 464 <ul> 465 <li> 466 <code>android.permission.CAMERA</code> 467 </li> 468 </ul> 469 </td> 470 </tr> 471 472 <tr> 473 <td><code>android.permission-group.CONTACTS</code></td> 474 <td> 475 <ul> 476 <li> 477 <code>android.permission.READ_CONTACTS</code> 478 </li> 479 <li> 480 <code>android.permission.WRITE_CONTACTS</code> 481 </li> 482 <li> 483 <code>android.permission.GET_ACCOUNTS</code> 484 </li> 485 </ul> 486 </td> 487 </tr> 488 489 <tr> 490 <td><code>android.permission-group.LOCATION</code></td> 491 <td> 492 <ul> 493 <li> 494 <code>android.permission.ACCESS_FINE_LOCATION</code> 495 </li> 496 <li> 497 <code>android.permission.ACCESS_COARSE_LOCATION</code> 498 </li> 499 </ul> 500 </td> 501 </tr> 502 503 <tr> 504 <td><code>android.permission-group.MICROPHONE</code></td> 505 <td> 506 <ul> 507 <li> 508 <code>android.permission.RECORD_AUDIO</code> 509 </li> 510 </ul> 511 </td> 512 </tr> 513 514 <tr> 515 <td><code>android.permission-group.PHONE</code></td> 516 <td> 517 <ul> 518 <li> 519 <code>android.permission.READ_PHONE_STATE</code> 520 </li> 521 <li> 522 <code>android.permission.CALL_PHONE</code> 523 </li> 524 <li> 525 <code>android.permission.READ_CALL_LOG</code> 526 </li> 527 <li> 528 <code>android.permission.WRITE_CALL_LOG</code> 529 </li> 530 <li> 531 <code>com.android.voicemail.permission.ADD_VOICEMAIL</code> 532 </li> 533 <li> 534 <code>android.permission.USE_SIP</code> 535 </li> 536 <li> 537 <code>android.permission.PROCESS_OUTGOING_CALLS</code> 538 </li> 539 </ul> 540 </td> 541 </tr> 542 543 <tr> 544 <td><code>android.permission-group.SENSORS</code></td> 545 <td> 546 <ul> 547 <li> 548 <code>android.permission.BODY_SENSORS</code> 549 </li> 550 </ul> 551 </td> 552 </tr> 553 554 <tr> 555 <td><code>android.permission-group.SMS</code></td> 556 <td> 557 <ul> 558 <li> 559 <code>android.permission.SEND_SMS</code> 560 </li> 561 <li> 562 <code>android.permission.RECEIVE_SMS</code> 563 </li> 564 <li> 565 <code>android.permission.READ_SMS</code> 566 </li> 567 <li> 568 <code>android.permission.RECEIVE_WAP_PUSH</code> 569 </li> 570 <li> 571 <code>android.permission.RECEIVE_MMS</code> 572 </li> 573 <li> 574 <code>android.permission.READ_CELL_BROADCASTS</code> 575 </li> 576 </ul> 577 </td> 578 </tr> 579 580 <tr> 581 <td> 582 <code>android.permission-group.STORAGE</code> 583 </td> 584 <td> 585 <ul> 586 <li> 587 <code>android.permission.READ_EXTERNAL_STORAGE</code> 588 </li> 589 <li> 590 <code>android.permission.WRITE_EXTERNAL_STORAGE</code> 591 </li> 592 </ul> 593 </td> 594 </tr> 595 596</table> 597 598<h4 id="explain-need">Explain why the app needs permissions</h4> 599 600<p> 601 In some circumstances, you might want to help the user understand why your 602 app needs a permission. For example, if a user launches a photography app, 603 the user probably won't be surprised that the app asks for permission to use 604 the camera. But if the user turns down that permission request, then launches 605 the photography app again, that might indicate that the user needs some help 606 understanding why the permission is needed. 607</p> 608 609<p> 610 To help find the situations where you need to provide extra explanation, the 611 system provides the {@link 612 android.app.Activity#shouldShowRequestPermissionRationale 613 shouldShowRequestPermissionRationale()} method. This method returns 614 <code>true</code> if the app has requested this permission previously and the 615 user denied the request. That indicates that you should probably explain to 616 the user why you need the permission. 617</p> 618 619<p> 620 If the user turned down the permission request in the past and chose the 621 <em>Don't ask again</em> option in the permission request system dialog, this 622 method returns <code>false</code>. The method also returns <code>false</code> 623 if the device policy prohibits the app from having that permission. 624</p> 625 626 627 628<h4 id="request-permissions">Request permissions if necessary</h4> 629 630<p>If the app doesn't already have the permission it needs, the app calls the 631 {@link android.app.Activity#requestPermissions requestPermissions()} method to 632 request the appropriate permission or permissions. The app passes the 633 permission or permissions it wants, and also an integer "request code". 634 This method functions asynchronously: it returns right away, and after 635 the user responds to the dialog box, the system calls the app's callback 636 method with the results, passing the same "request code" that the app passed 637 to {@link android.app.Activity#requestPermissions requestPermissions()}.</p> 638 639 <p>The following code code checks if the app has permission to read the 640 user's contacts, and requests the permission if necessary:</p> 641 642<pre> 643if (checkSelfPermission(Manifest.permission.READ_CONTACTS) 644 != PackageManager.PERMISSION_GRANTED) { 645 646 // Should we show an explanation? 647 if (shouldShowRequestPermissionRationale( 648 Manifest.permission.READ_CONTACTS)) { 649 // Explain to the user why we need to read the contacts 650 } 651 652 requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, 653 MY_PERMISSIONS_REQUEST_READ_CONTACTS); 654 655 // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an 656 // app-defined int constant 657 658 return; 659} 660</pre> 661 662<p class="note"> 663 <strong>Note:</strong> When your app calls the framework's {@link 664 android.app.Activity#requestPermissions requestPermissions()} method, the 665 system shows a standard dialog box to the user. Your app <em>cannot</em> 666 configure or alter that dialog box. If you need to provide any information or 667 explanation to the user, you should do that <em>before</em> you call {@link 668 android.app.Activity#requestPermissions requestPermissions()}, as described 669 in <a href="#explain-need">Explain why the app needs permissions</a>. 670</p> 671 672<h4 id="handle-response">Handle the permissions request response</h4> 673 674<p> 675 When an app requests permissions, the system presents a dialog box to the 676 user. When the user responds, the system invokes your app's {@link 677 android.app.Activity#onRequestPermissionsResult} passing it the user 678 response. Your app needs to override that method. The callback is passed the 679 same request code you passed to {@link 680 android.app.Activity#requestPermissions requestPermissions()}. For example, 681 if an app requests <code>READ_CONTACTS</code> access it might have the 682 following callback method: 683</p> 684 685<pre> 686@Override 687public void onRequestPermissionsResult(int requestCode, 688 String permissions[], int[] grantResults) { 689 switch (requestCode) { 690 case MY_PERMISSIONS_REQUEST_READ_CONTACTS: { 691 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 692 693 // permission was granted, yay! do the 694 // calendar task you need to do. 695 696 } else { 697 698 // permission denied, boo! Disable the 699 // functionality that depends on this permission. 700 } 701 return; 702 } 703 704 // other 'switch' lines to check for other 705 // permissions this app might request 706 } 707} 708</pre> 709 710<p> 711 If the user denies a permission request, your app should take appropriate 712 action. For example, your app might show a dialog explaining why it could not 713 perform the user's original request. 714</p> 715 716<p> 717 When the system asks the user to grant a permission, the user has the option 718 of telling the system not to ask for that permission again. In that case, 719 when an app uses {@link android.app.Activity#requestPermissions 720 requestPermissions()} to ask for that permission, the system immediately 721 denies the request. In this case, the system calls your {@link 722 android.app.Activity#onRequestPermissionsResult onRequestPermissionsResult()} 723 the same way it would if the user had explicitly rejected your request again. 724 For this reason, your app cannot assume that any direct interaction with the 725 user has taken place. 726</p> 727 728<h2 id="testing">Testing Runtime Permissions</h2> 729 730<p> 731 If your app targets the M Developer Preview, you must test that it 732 handles permissions properly. You cannot assume that your app has any 733 particular permissions when it runs. When the app is first launched, it is 734 likely to have no permissions, and the user can revoke or restore permissions 735 at any time. 736</p> 737 738<p> 739 You should test your app to make sure it behaves properly under all 740 permission situations. With the M Preview SDK, we have provided new 741 <a href="{@docRoot}tools/help/adb.html">Android 742 Debug Bridge (adb)</a> commands to enable you to test your app with whatever 743 permissions settings you need to try. 744</p> 745 746<h3> 747 New adb commands and options 748</h3> 749 750<p> 751 The M Preview SDK Platform-tools provides several new commands to let you test 752 how your app handles permissions. 753</p> 754 755<h4> 756 Install with permissions 757</h4> 758 759<p> 760 You can use the <a href="{@docRoot}tools/help/adb.html#move"><code>adb 761 install</code></a> command's new <code>-g</code> option, which installs the 762 app and grants all permissions listed in its manifest: 763</p> 764 765<pre class="no-pretty-print"> 766$ adb install -g <path_to_apk> 767</pre> 768 769<h4> 770 Grant and revoke permissions 771</h4> 772 773<p> 774 You can use new ADB <a href="{@docRoot}tools/help/adb.html#pm">package manager 775 (pm)</a> commands to grant and revoke permissions to an installed app. 776 This functionality can be useful for automated testing. 777</p> 778 779<p> 780 To grant a permission, use the package manager's <code>grant</code> command: 781</p> 782 783<pre class="no-pretty-print"> 784$ adb pm grant <package_name> <permission_name> 785</pre> 786 787<p> 788 For example, to grant the com.example.myapp package permission to record 789 audio, use this command: 790</p> 791 792<pre class="no-pretty-print"> 793$ adb pm grant com.example.myapp android.permission.RECORD_AUDIO 794</pre> 795 796<p> 797 To revoke a permission, use the package manager's <code>revoke</code> command: 798</p> 799 800<pre class="no-pretty-print"> 801$ adb pm revoke <package_name> <permission_name> 802</pre> 803 804<h2 id="best-practices">Best Practices and Usage Notes</h2> 805 806<p> 807 The new permissions model gives users a smoother experience, and makes it 808 easier for them to install apps and feel comfortable with what the apps are 809 doing. We recommend the following best practices to take full advantage of 810 the new model. 811</p> 812 813 814<h3 id="bp-what-you-need">Only ask for permissions you need</h3> 815 816<p> 817 Every time you ask for a permission, you force the user to make a decision. 818 If the user turns down the request, that reduces your app's functionality. 819 You should minimize the number of times you make these requests. 820</p> 821 822<p> 823 For example, quite often your app can get needed functionality by using an 824 <a href="{@docRoot}guide/components/intents-filters.html">intent</a> instead 825 of asking for permissions. If your app needs to take pictures with the 826 phone's camera, your app can use a {@link 827 android.provider.MediaStore#ACTION_IMAGE_CAPTURE 828 MediaStore.ACTION_IMAGE_CAPTURE} intent. When your app executes the intent, the 829 system prompts the user to choose an already-installed camera app to take the 830 picture. 831</p> 832 833<h3 id="bp-dont-overwhelm"> 834 Don't overwhelm the user 835</h3> 836 837<p> 838 If you confront the user with a lot of requests for permissions at once, you may 839 overwhelm the user and cause them to quit your app. Instead, you should ask 840 for permissions as you need them. 841</p> 842 843<p> 844 In some cases, one or more permissions might be absolutely essential to your 845 app. In that case, it might make sense to ask for all the permissions as soon 846 as the app launches. For example, if you make a photography app, the app 847 would need access to the device camera. When the user launches the app for 848 the first time, they won't be surprised to be asked for permission to use 849 the camera. But if the same app also had a feature to share photos with the 850 user's contacts, you probably should <em>not</em> ask for that permission at 851 first launch. Instead, wait until the user tries to use the "sharing" feature 852 and ask for the permission then. 853</p> 854 855<p> 856 If your app provides a tutorial, it may make sense to request the app's essential 857 permissions at the end of the tutorial sequence. 858</p> 859 860<h3 id="bp-explain"> 861 Explain why you need permissions 862</h3> 863 864<p> 865 The permissions dialog shown by the system when you call {@link 866 android.app.Activity#requestPermissions requestPermissions()} says what 867 permission your app wants, but doesn't say why. In some cases, the user may 868 find that puzzling. It's a good idea to explain to the user why your app 869 wants the permissions before calling {@link 870 android.app.Activity#requestPermissions requestPermissions()}. 871</p> 872 873<p> 874 For example, a photography app might want to use location services, so it can 875 geotag the photos. A typical user might not understand that a photo can 876 contain location information, and would be puzzled why their photography app 877 wanted to know the location. So in this case, it's a good idea for the app to 878 tell the user about this feature <em>before</em> calling 879 {@link android.app.Activity#requestPermissions requestPermissions()}. 880</p> 881 882<p> 883 One way to do this is to incorporate these requests into an app tutorial. The 884 tutorial can show each of the app's features in turn, and as it does this, it 885 can explain what permissions are needed. For example, the photography app's 886 tutorial can demonstrate its "share photos with your contacts" feature, then 887 tell the user that they need to give permission for the app to see the user's 888 contacts. The app can then call {@link 889 android.app.Activity#requestPermissions requestPermissions()} to ask the user 890 for that access. Of course, not every user is going to follow the tutorial, 891 so you still need to check for and request permissions during the app's 892 normal operation. 893</p> 894 895<h3 id="support-lib">Support library methods for handling permissions</h3> 896 897<p> 898 Revision 23 of the v4 and v13 support libraries provide several new methods 899 for managing permissions. The support library methods work properly on any 900 device that can use those libraries. Thus, if you use the support library 901 methods, you do not need to check whether your app is running on a device 902 with the M Developer Preview. If an app is installed on a device running the 903 M Preview, the support library methods behave the same as their framework 904 equivalents. If the device is running an earlier version of Android, the 905 methods behave appropriately, as described below. 906</p> 907 908<p> 909 The v4 support library provides the following permissions methods: 910</p> 911 912<dl> 913 <dt> 914 {@link android.support.v4.content.ContextCompat#checkSelfPermission 915 ContextCompat.checkSelfPermission()} 916 </dt> 917 918 <dd> 919 Returns {@link android.content.pm.PackageManager#PERMISSION_GRANTED 920 PERMISSION_GRANTED} if the app has the specified permission, whether 921 or not the device is using the M Preview. If the app does not have the 922 specified permission, returns {@link 923 android.content.pm.PackageManager#PERMISSION_DENIED PERMISSION_DENIED}. 924 </dd> 925 926 <dt> 927 {@link android.support.v4.app.ActivityCompat#requestPermissions 928 ActivityCompat.requestPermissions()} 929 </dt> 930 931 <dd> 932 If the device is not running the M Preview, invokes the callback 933 method in {@link 934 android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback}. 935 Passes {@link android.content.pm.PackageManager#PERMISSION_GRANTED 936 PERMISSION_GRANTED} if the app already has the specified permission, or 937 {@link android.content.pm.PackageManager#PERMISSION_DENIED 938 PERMISSION_DENIED} if it does not. 939 </dd> 940 941 <dt> 942 {@link 943 android.support.v4.app.ActivityCompat#shouldShowRequestPermissionRationale 944 ActivityCompat.shouldShowRequestPermissionRationale()} 945 </dt> 946 947 <dd> 948 If the device is not running the M Preview, always returns 949 <code>false</code>. 950 </dd> 951</dl> 952 953<p> 954 The v4 support library also contains the 955 {@link android.support.v4.content.PermissionChecker} 956 class, which provides several static utility methods for apps that use IPC to 957 provide services for other apps. For example, 958 {@link android.support.v4.content.PermissionChecker#checkCallingPermission 959 PermissionChecker.checkCallingPermission()} 960 checks whether an IPC 961 made by a particular package has a specified permission. 962</p> 963 964<p class="note"> 965 <strong>Note:</strong> If your app acts on behalf of third-party apps to call 966 platform methods that require runtime permissions on behalf of a third-party 967 app, you should use the appropriate {@link 968 android.support.v4.content.PermissionChecker} methods to ensure that the 969 other app is allowed to perform the operation. The platform has a 970 compatibility mode that allows users to revoke a legacy app's access to 971 permission-protected methods. If the user revokes access in compatibility 972 mode the app's permissions are not actually revoked; instead, access to the 973 APIs is restricted. The {@link android.support.v4.content.PermissionChecker} 974 methods verify app permissions in both normal and legacy modes. 975</p> 976 977<p> 978 The v13 support library provides the following permissions methods: 979</p> 980 981<dl> 982 <dt> 983 {@link android.support.v13.app.FragmentCompat#requestPermissions 984 FragmentCompat.requestPermissions()} 985 </dt> 986 987 <dd> 988 If the device is not running the M Preview, invokes the callback 989 method in <code>FragmentCompat.OnRequestPermissionsResultCallback</code>. 990 Passes {@link android.content.pm.PackageManager#PERMISSION_GRANTED 991 PERMISSION_GRANTED} if the app already has the specified permission, or 992 {@link android.content.pm.PackageManager#PERMISSION_DENIED 993 PERMISSION_DENIED} if it does not. 994 </dd> 995 996 <dt> 997 {@link 998 android.support.v13.app.FragmentCompat#shouldShowRequestPermissionRationale 999 FragmentCompat.shouldShowRequestPermissionRationale()} 1000 </dt> 1001 1002 <dd> 1003 If the device is not running the M Preview, always returns 1004 <code>false</code>. 1005 </dd> 1006</dl> 1007 1008<h3 id="normal">Normal permissions</h3> 1009 1010<p> 1011 Many permissions are designated as {@link 1012 android.content.pm.PermissionInfo#PROTECTION_NORMAL PROTECTION_NORMAL}, 1013 which indicates that 1014 there's no great risk to the user's privacy or security in letting apps have 1015 those permissions. For example, users would reasonably want to know whether 1016 an app can read their contact information, so users have to grant this 1017 permission explicitly. By contrast, there's no great risk in allowing an app 1018 to vibrate the device, so that permission is designated as <em>normal.</em> 1019</p> 1020 1021<p> 1022 If an app declares in its 1023 manifest that it needs a normal permission, the system automatically grants 1024 the app 1025 that permission at install time. The system does not prompt the user 1026 to grant normal 1027 permissions, and users cannot revoke these permissions. 1028</p> 1029 1030<p> 1031 If your app declares that it needs normal permissions, the app does not need 1032 to call {@link android.content.Context#checkSelfPermission 1033 checkSelfPermission()} or {@link android.app.Activity#requestPermissions 1034 requestPermissions()} for those permissions. Since you declared the 1035 permissions in the manifest, you can be sure your app was granted those 1036 permissions at install time. 1037</p> 1038 1039<p>Currently, the following permissions are classified as {@link 1040 android.content.pm.PermissionInfo#PROTECTION_NORMAL PROTECTION_NORMAL}:</p> 1041 1042<ul> 1043 <li><code>android.permission.ACCESS_LOCATION_EXTRA_COMMANDS</code></li> 1044 <li><code>android.permission.ACCESS_NETWORK_STATE</code></li> 1045 <li><code>android.permission.ACCESS_NOTIFICATION_POLICY</code></li> 1046 <li><code>android.permission.ACCESS_WIFI_STATE</code></li> 1047 <li><code>android.permission.ACCESS_WIMAX_STATE</code></li> 1048 <li><code>android.permission.BLUETOOTH</code></li> 1049 <li><code>android.permission.BLUETOOTH_ADMIN</code></li> 1050 <li><code>android.permission.BROADCAST_STICKY</code></li> 1051 <li><code>android.permission.CHANGE_NETWORK_STATE</code></li> 1052 <li><code>android.permission.CHANGE_WIFI_MULTICAST_STATE</code></li> 1053 <li><code>android.permission.CHANGE_WIFI_STATE</code></li> 1054 <li><code>android.permission.CHANGE_WIMAX_STATE</code></li> 1055 <li><code>android.permission.DISABLE_KEYGUARD</code></li> 1056 <li><code>android.permission.EXPAND_STATUS_BAR</code></li> 1057 <li><code>android.permission.FLASHLIGHT</code></li> 1058 <li><code>android.permission.GET_ACCOUNTS</code></li> 1059 <li><code>android.permission.GET_PACKAGE_SIZE</code></li> 1060 <li><code>android.permission.INTERNET</code></li> 1061 <li><code>android.permission.KILL_BACKGROUND_PROCESSES</code></li> 1062 <li><code>android.permission.MODIFY_AUDIO_SETTINGS</code></li> 1063 <li><code>android.permission.NFC</code></li> 1064 <li><code>android.permission.READ_SYNC_SETTINGS</code></li> 1065 <li><code>android.permission.READ_SYNC_STATS</code></li> 1066 <li><code>android.permission.RECEIVE_BOOT_COMPLETED</code></li> 1067 <li><code>android.permission.REORDER_TASKS</code></li> 1068 <li><code>android.permission.REQUEST_INSTALL_PACKAGES</code></li> 1069 <li><code>android.permission.SET_TIME_ZONE</code></li> 1070 <li><code>android.permission.SET_WALLPAPER</code></li> 1071 <li><code>android.permission.SET_WALLPAPER_HINTS</code></li> 1072 <li><code>android.permission.SUBSCRIBED_FEEDS_READ</code></li> 1073 <li><code>android.permission.TRANSMIT_IR</code></li> 1074 <li><code>android.permission.USE_FINGERPRINT</code></li> 1075 <li><code>android.permission.VIBRATE</code></li> 1076 <li><code>android.permission.WAKE_LOCK</code></li> 1077 <li><code>android.permission.WRITE_SYNC_SETTINGS</code></li> 1078 <li><code>com.android.alarm.permission.SET_ALARM</code></li> 1079 <li><code>com.android.launcher.permission.INSTALL_SHORTCUT</code></li> 1080 <li><code>com.android.launcher.permission.UNINSTALL_SHORTCUT</code></li> 1081</ul> 1082