android監聽SIM狀態

來源:互聯網
上載者:User

android監聽SIM狀態

  1. /*
  2. 監聽sim狀態改變的廣播,返回sim卡的狀態, 有效或者無效。
  3. 雙卡中只要有一張卡的狀態有效即返回狀態為有效,兩張卡都無效則返回無效。
  4. */
  5. import android.app.Service;
  6. import android.content.BroadcastReceiver;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.telephony.TelephonyManager;
  10.  
  11. public class SimStateReceive extends BroadcastReceiver {
  12. private final static String ACTION_SIM_STATE_CHANGED = "android.intent.action.SIM_STATE_CHANGED";
  13. private final static int SIM_VALID = 0;
  14. private final static int SIM_INVALID = 1;
  15. private int simState = SIM_INVALID;
  16.  
  17. public int getSimState() {
  18. return simState;
  19. }
  20.  
  21. @Override
  22. public void onReceive(Context context, Intent intent) {
  23. if (intent.getAction().equals(ACTION_SIM_STATE_CHANGED)) {
  24. TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
  25. int state = tm.getSimState();
  26. switch (state) {
  27. case TelephonyManager.SIM_STATE_READY :
  28. simState = SIM_VALID;
  29. break;
  30. case TelephonyManager.SIM_STATE_UNKNOWN :
  31. case TelephonyManager.SIM_STATE_ABSENT :
  32. case TelephonyManager.SIM_STATE_PIN_REQUIRED :
  33. case TelephonyManager.SIM_STATE_PUK_REQUIRED :
  34. case TelephonyManager.SIM_STATE_NETWORK_LOCKED :
  35. default:
  36. simState = SIM_INVALID;
  37. break;
  38. }
  39. }
  40. }
  41.  
  42. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.