2015-06-15 107 views
-10

我試圖讓設備ID下面的代碼如何解決錯誤

public String getDevId() { 
     String devId=android.telephony.TelephonyManager.getDeviceId(); 
     return devId; 
    } 

但它說:「非靜態方法「非靜態方法‘getDeviceId()’不能由靜態上下文中引用」 getDeviceId()'不能被靜態上下文引用「

+0

首先清理你的核心Java。 – Arunkumar

+0

我做了tryTelephonyManager xx = new TelephonyManager();但它是說「TelephonyManager()」不是公開的 –

+0

包含你的logcat,所以我們可以看到發生了什麼 –

回答

4
public String getDeviceId(Context context){ 
    TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
     return telephonyManager.getDeviceId(); 
} 
+0

它給錯誤無法解析java.lang.string中的getSystemService –

+0

你需要一個上下文來訪問getSystemService – Blackbelt

+0

爲什麼你直接跳轉到TelephonyManager。首先從[Android入門]開始(http://developer.android.com/guide/index.html) – Vivart