Android Monitor SIM card no service sample (SIM card management) _android

Source: Internet
Author: User


Copy Code code as follows:

public class Uniteqservice {
public static final String Log_tag = "Uniteqservice";
public Boolean DEBUG = true;
Private Boolean hasservice = false;//whether there is a service
Private phonestatelistener[] mphonestatelistener;//monitor dual SIM card
Private Telephonymanager Mphone;

Listening class
Private Phonestatelistener Getphonestatelistener (final int sub) {
Phonestatelistener Sphonestatelistener = new Phonestatelistener (sub) {
@Override
public void onservicestatechanged (ServiceState servicestate) {
TODO auto-generated Method Stub
if (servicestate!= null) {
if (servicestate.getstate () = = Servicestate.state_in_service) {
Hasservice = true;
} else {
Hasservice = false;
}
} else {
if (DEBUG) log.d (TAG, "No service state...may be the balance are not enough" + sub);
}
Super.onservicestatechanged (servicestate);
}
};
return sphonestatelistener;
}
Registering for listening
private void Registerphonelistener () {
Get the card numbers, Msimtelephonymanager This class API is not open, you can compile the source code
Get the Frameworks.jar package and import your project, then import Android.telephony.MSimTelephonyManager;
Msub = Msimtelephonymanager.getdefault (). Getphonecount ();
Mphonestatelistener = new Phonestatelistener[msub];
for (int i = 0; i < msub; i++) {
Mphonestatelistener[i] = Getphonestatelistener (i);
Mphone.listen (Mphonestatelistener[i], phonestatelistener.listen_service_state);
}
}

Cancel Listening
private void Unregisterphonelistener () {
Msub = Msimtelephonymanager.getdefault (). Getphonecount ();
for (int i = 0; i < msub; i++) {
if (Mphonestatelistener[i]!= null) {
Mphone.listen (Mphonestatelistener[i], phonestatelistener.listen_none);
}
}
}
}

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.