2016-02-14 55 views
1

我能夠使用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' 

它會工作,如果我圍繞它與一個嘗試緩存塊?

回答

0
String simID = null; 
try{ 
    simID = tm.getSimSerialNumber(); 
}catch(Exception e){ 
}finally{ 
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(); 
} 
+0

SIMID不能被解析爲一個變量 – Felicia

+0

TM解決不了 – Felicia

+0

其投擲空當它不具有SIM卡 – dileep

0

你在你的按鈕,通過view爲參數刪除參數,一切都應該工作的onclick方法調用getAndSaveCurrentID(View)。您的方法getAndSaveCurrentID()沒有任何參數。

private void getAndSaveCurrentID() { 
    try{ 
     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(); 
    }catch(Exception e){ 
     Log.e("Error","error ocurred"); 
    } 
} 

希望這有助於

+0

我知道,我刪除它只是爲了測試異常handelling,它強制關閉,如果沒有SIM卡卡號 – Felicia

+0

你的答案不起作用,請檢查我的卡號 – Felicia

1

您可以檢查SIM不存在或不是。

TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
     if(telephonyManager.getSimState()!= TelephonyManager.SIM_STATE_ABSENT) 
     { 
      //your code here 
     } 
0

這裏是我的sollution,它檢測是否第一有一個SIM卡,然後調用我的方法,隨意編輯我的答案!

public void SimDetectorForGetAndSaveCurrentId(View view) {// save serial 
    // fire only if there is sim card inserted 
    TelephonyManager telMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
    int simState = telMgr.getSimState(); 
    switch (simState) { 
    case TelephonyManager.SIM_STATE_ABSENT: 
     Toast.makeText(getApplicationContext(), 
       "please make sure you have a SIM card inserted", Toast.LENGTH_LONG) 
       .show(); 
     break; 
    case TelephonyManager.SIM_STATE_NETWORK_LOCKED: 
     getAndSaveCurrentID(); 
     break; 
    case TelephonyManager.SIM_STATE_PIN_REQUIRED: 
     getAndSaveCurrentID(); 
     break; 
    case TelephonyManager.SIM_STATE_PUK_REQUIRED: 
     getAndSaveCurrentID(); 
     break; 
    case TelephonyManager.SIM_STATE_READY: 
     getAndSaveCurrentID(); 
     break; 
    case TelephonyManager.SIM_STATE_UNKNOWN: 
     Toast.makeText(getApplicationContext(), 
       "please make sure you have a SIM card inserted", Toast.LENGTH_LONG) 
       .show(); 
     break; 
    } 
} 
+0

你用這段代碼得到了什麼異常?請張貼您的logcat或您收到第一張貼:( –