Android listens to SIM status

Source: Internet
Author: User

Android listens to SIM status

  1. /*
  2. Listen to the broadcasting of sim status changes, and return the sim card status, valid or invalid.
  3. If either card is in valid status, the system returns valid status. If both cards are invalid, the system returns invalid status.
  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. }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.