手機在生產時,每部手機均有一個惟一的標識(ID),國際上採用國際移動設備身份碼(IMEI, International Mobile Equipment Identity)。IMEI是由15位數字組成的"電子串號",它與每臺手機一一對應,並且該碼是全世界惟一的。每一隻手機在組裝完成後都將被賦予一個全球惟一的一組號碼,這個號碼從生產到交付使用都將被製造生產的廠商所記錄。
在手機應用開發中,使用IMEI來作身份認證是一個經常使用的技術手段,在Android SDK中,類android.telephony.TelephonyManager提供了手機設備信息的相關操做和管理。
一、在AndroidManifest.xml中增長訪問設備狀態的權限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
二、經過上下文設備獲取到TelephonyManager實例,調用getDeviceId方法獲取IMEI:
import android.telephony.*;
……
TelephonyManager telephonyManager=(TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
String imei=telephonyManager.getDeviceId();
值得說明的是,在模擬器中運行時,getDeviceId方法返回老是000000000000000。android
另外,TelephonyManager類還提供了獲取手機其餘信息的方法,如:app
在Android 獲取手機信息的時候用到這樣一段代碼:函數
public class BasicInfo {ui
public String getPhoneNumber()this
{spa
// 獲取手機號 MSISDN,極可能爲空操作系統
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);.net
StringBuffer inf = new StringBuffer();code
switch(tm.getSimState()){ //getSimState()取得sim的狀態 有下面6中狀態 xml
case TelephonyManager.SIM_STATE_ABSENT :inf.append("無卡");return inf.toString();
case TelephonyManager.SIM_STATE_UNKNOWN :inf.append("未知狀態");return inf.toString();
case TelephonyManager.SIM_STATE_NETWORK_LOCKED :inf.append("須要NetworkPIN解鎖");return inf.toString();
case TelephonyManager.SIM_STATE_PIN_REQUIRED :inf.append("須要PIN解鎖");return inf.toString();
case TelephonyManager.SIM_STATE_PUK_REQUIRED :inf.append("須要PUK解鎖");return inf.toString();
case TelephonyManager.SIM_STATE_READY :break;
}
String phoneNumber = tm.getLine1Number();
return phoneNumber;
}
......
}
在另一個activity類裏面調用的時候 老是出現進程關閉 沒法獲取手機信息。
後來發現
|
因而:
1.
給BasicInfo 添加一個帶參數Context的構造函數:
public BasicInfo (Context context)
{
this.context = context;
}
2.
getPhoneNumber()函數裏面改爲:
context.getSystemService(Context.TELEPHONY_SERVIC);
3.
在調用類裏面 BasicInfo bi = new BasicInfo(this);
bi.getPhoneNumber();
問題解決。。。