Android 6 getAccountName() Missing android.permission.GET_ACCOUNTS

▼魔方 西西 提交于 2019-12-21 07:13:21

问题


I get the following Exception while running on a Android 6 device.

java.lang.SecurityException: Missing android.permission.GET_ACCOUNTS

This looks like a fairly straight forward exception, but for me it's not. My manifest looks like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="..." >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />

<application
    ...

The piece of code which throws this exception is the following line:

mLoggedInEmail = Plus.AccountApi.getAccountName(mGoogleApiClient);

On an android 5 device it works just fine.


回答1:


Starting with Android M, API 23, you need to request some permission at runtime on top of declaring them in your manifest.

See this link for more info: https://developer.android.com/training/permissions/requesting.html

As a quick workaround, you can set your targetSdkVersion to 22 in your build.gradle.

Keep in mind that even with this workaround, if the user goes in setting in disable the permission for your app, your app will crash.

This is the list of permissions impacted by this new change:

  • ACCESS_COARSE_LOCATION
  • ACCESS_FINE_LOCATION
  • ADD_VOICEMAIL
  • BODY_SENSORS
  • CALL_PHONE
  • CAMERA
  • GET_ACCOUNTS
  • PROCESS_OUTGOING_CALLS
  • READ_CALENDAR
  • READ_CALL_LOG
  • READ_CELL_BROADCASTS
  • READ_CONTACTS
  • READ_EXTERNAL_STORAGE
  • READ_PHONE_STATE
  • READ_SMS
  • RECEIVE_MMS
  • RECEIVE_SMS
  • RECEIVE_WAP_PUSH
  • RECORD_AUDIO
  • SEND_SMS
  • USE_SIP
  • WRITE_CALENDAR
  • WRITE_CALL_LOG
  • WRITE_CONTACTS
  • WRITE_EXTERNAL_STORAGE


来源:https://stackoverflow.com/questions/33023674/android-6-getaccountname-missing-android-permission-get-accounts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!