RT, 異常信息如下:
java.lang.SecurityException: getDeviceId: Neither user 10065 nor current process has android.permission.READ_PHONE_STATE.
PS:
Android Studio 版本:2.3.3
模擬器環境:Genymotion, Android 7.1.1, API 25
原因:動態權限的問題
Android 6.0 (API Level 23) 及以后引入了運行時權限,安裝 APP 時不再授予所有需要的權限,而是在運行的時候根據需要向用戶請求權限,而且用戶可以隨時取消已經授予的權限。
推薦開源庫:
GitHub 上有個對開源庫 https://github.com/googlesamples/easypermissions 進行二次封裝的開源庫 https://github.com/forJrking/HeiPermission 還不錯,可以嘗試下。
參考鏈接:
http://blog.csdn.net/github_33304260/article/details/53328297
https://developer.android.com/training/permissions/requesting.html