2017-03-08 88 views
1

我有問題,UUID Android.This方法獲得UUID Android和增加許可清單獲取UUID的Android

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 

這種方法得到這個UUID

public String deviceUDID(Context ctx) { 
     final TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE); 

     final String tmDevice, tmSerial, androidId; 
     tmDevice = "" + tm.getDeviceId(); //line 82 
     tmSerial = "" + tm.getSimSerialNumber(); 
     androidId = "" + android.provider.Settings.Secure.getString(ctx.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID); 

     UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode()); 
     String deviceId = deviceUuid.toString(); 
     Log.d("Device Id", deviceId); 
     return deviceId; 
    } 

當點擊按鈕與展會上的TextView UUID ,應用程序停止。

我的錯誤日誌

03-08 14:22:39.496 20825-20825/com.novum.smrtkarta E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.xxxx.smrtkarta, PID: 20825 
java.lang.SecurityException: getDeviceId: Neither user 10155 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:4207) 
at android.telephony.TelephonyManager.getDeviceId(TelephonyManager.java:706) 
at com.novum.smrtkarta.activity.GetNumberActivity.deviceUDID(GetNumberActivity.java:81) 
at com.novum.smrtkarta.activity.GetNumberActivity$1.onClick(GetNumberActivity.java:62) 
at android.view.View.performClick(View.java:5204) 
at android.view.View$PerformClick.run(View.java:21153) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
+0

看起來讀取權限問題 –

回答

1

你必須確保你的請求在您的清單中的權限READ_PHONE_STATE

<manifest xmlns:android="schemas.android.com/apk/res/android"; package="your.package.name"> 

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 

但是,如果你的目標版本的Android M,宣佈在許可Manifest將不夠,您必須在運行時明確詢問用戶: https://developer.android.com/training/permissions/requesting.html

要檢查您是否有權限,請致電 ContextCompat.checkSelfPermission()方法。例如,該 片斷顯示瞭如何檢查活動有寫權限 日曆:

// Assume thisActivity is the current activity 
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_CALENDAR); 

所以你的情況,調用該方法deviceUDID之前,請確保您有READ_PHONE_STATE權限。

您可以按照指示的步驟here

+0

我加了這個權限 – Ewelina

+0

@Ewelina你能指出你正在運行的Android版本嗎? – w00ly

+0

@Ewelina我已經通過了你的帖子的編輯,發現你已經提到你添加了UUID清單中的權限編輯你的問題。 –