使用telephonymanager

时间:2016-02-14 05:55:35

标签: android

我可以使用telephonymanager获取我的SIM卡的序列号,代码工作正常。但是,如果没有插入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'

如果我用try缓存块包围它会有效吗?

4 个答案:

答案 0 :(得分:1)

您可以检查SIM卡是否缺席。

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

答案 1 :(得分: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();
}

答案 2 :(得分:0)

您在按钮的onClick方法中调用getAndSaveCurrentID(View) 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");
    }
}

希望这有帮助

答案 3 :(得分:0)

这是我的解决方案,它首先检测是否有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;
    }
}