Android 6.0獲取IMEI號是出錯,動態獲取許可權
阿新 • • 發佈:2018-12-30
之前更新了一個版本,獲取使用者的IMEI裝置號,本地手機測試沒問題,就放到伺服器上,結果有很多使用者反應,應用打不開。也不是全部使用者,只有少部分Android 6.0系統的使用者和一些root過的使用者,由於那不到使用者手機,只能從錯誤日誌中檢視。
uses-permission android:name="android.permission.READ_PHONE_STATE" />
然後在程式碼中新增:
出錯日誌:
可以看到,是在getDeviceId的時候出錯:java.lang.SecurityException: getDeviceId: Neither user 10147 nor current process has android.permission.READ_PHONE_STATE. 網上說沒有加許可權,但是我在AndroidManifast.xml中確實是設定了許可權:<uses-permission android:name="android.permission.READ_PHONE_STATE" />。但是並沒有起作用。只能動態獲取了: 首先在AndroidManifest.xml中新增許可權<SUPPORTED_64_BIT_ABIS=[Ljava.lang.String;@cdce4d6 versionCode=42 BOARD=MSM8974 BOOTLOADER=unknown TYPE=user ID=MMB29M TIME=1458153356000 BRAND=Xiaomi TAG=Build SERIAL=953692f1 HARDWARE=qcom SUPPORTED_ABIS=[Ljava.lang.String;@b4ad357 CPU_ABI=armeabi-v7a RADIO=unknown IS_DEBUGGABLE=true MANUFACTURER=Xiaomi SUPPORTED_32_BIT_ABIS=[Ljava.lang.String;@85460f1 TAGS=release-keys CPU_ABI2=armeabi UNKNOWN=unknown USER=builder FINGERPRINT=Xiaomi/cancro_wc_lte/cancro:6.0.1/MMB29M/6.3.17:user/release-keys HOST=qh-miui-ota-bd55 PRODUCT=cancro_wc_lte versionName=2.5.0 DISPLAY=MMB29M MODEL=MI 4LTE DEVICE=cancro java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jshon.perdate/com.jshon.perdate.activity.SplashActivity}: java.lang.SecurityException: getDeviceId: Neither user 10147 nor current process has android.permission.READ_PHONE_STATE. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2421) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481) at android.app.ActivityThread.access$900(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5432) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:735) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) Caused by: java.lang.SecurityException: getDeviceId: Neither user 10147 nor current process has android.permission.READ_PHONE_STATE. at android.os.Parcel.readException(Parcel.java:1620) at android.os.Parcel.readException(Parcel.java:1573) at com.android.internal.telephony.ITelephony$Stub$Proxy.getDeviceId(ITelephony.java:4549) at android.telephony.TelephonyManager.getDeviceId(TelephonyManager.java:711) at com.jshon.perdate.activity.SplashActivity.onCreate(SplashActivity.java:179) at android.app.Activity.performCreate(Activity.java:6303) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2374) ... 9 more java.lang.SecurityException: getDeviceId: Neither user 10147 nor current process has android.permission.READ_PHONE_STATE. at android.os.Parcel.readException(Parcel.java:1620) at android.os.Parcel.readException(Parcel.java:1573) at com.android.internal.telephony.ITelephony$Stub$Proxy.getDeviceId(ITelephony.java:4549) at android.telephony.TelephonyManager.getDeviceId(TelephonyManager.java:711) at com.jshon.perdate.activity.SplashActivity.onCreate(SplashActivity.java:179) at android.app.Activity.performCreate(Activity.java:6303) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2374) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481) at android.app.ActivityThread.access$900(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5432) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:735) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
//Android6.0需要動態獲取許可權 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { // toast("需要動態獲取許可權"); ActivityCompat.requestPermissions(SplashActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_PHONE_STATE); }else{ // toast("不需要動態獲取許可權"); TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); Contants.IMEI = TelephonyMgr.getDeviceId(); } /** *加個獲取許可權的監聽 */ @Override public void onRequestPermissionsResult(int requestCode, String[] permissions,int[] grantResults) { if (requestCode == REQUEST_PHONE_STATE && grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); Contants.IMEI = TelephonyMgr.getDeviceId(); } }