我能夠使用telephonymanager獲得sim卡的序列號,代碼正常工作。但是,如果沒有插入SIM卡,它會關閉,那麼有什麼辦法來處理這個異常?使用telephonymanager獲取sim序列號
private void getAndSaveCurrentID(View view) {
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// ---get first time SIM card ID ---
String simID = tm.getSimSerialNumber();
if (simID != null)
Toast.makeText(this, "current SIM card ID: saved " + simID,
Toast.LENGTH_LONG).show();
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Name", simID.toString());
editor.commit();
}
02-14 11:27:42.643: E/AndroidRuntime(9674): FATAL EXCEPTION: main
02-14 11:27:42.643: E/AndroidRuntime(9674): Process: com.air.bgusatm, PID: 9674
02-14 11:27:42.643: E/AndroidRuntime(9674): java.lang.IllegalStateException: Could not find a method getAndSaveCurrentID(View) in the activity class com.air.bgusatm.MainActivity for onClick handler on view class android.widget.Button with id 'button1'
它會工作,如果我圍繞它與一個嘗試緩存塊?
SIMID不能被解析爲一個變量 – Felicia
TM解決不了 – Felicia
其投擲空當它不具有SIM卡 – dileep