Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 判斷 SIM 卡所屬的電信運營商

編輯:關於 Android 程式設計

在 Android 中,可以透過 TelephonyManager 取得本機的網路提供者資訊。原理是讀取 SIM 卡的 IMSI 碼,再依其前綴判斷所屬的電信業者。以下為實作程式碼:

public String getProvidersName()
{
    String str = "N/A";
    try
    {
        this.IMSI = this.telephonyManager.getSubscriberId();
        System.out.println(this.IMSI);
        if (this.IMSI.startsWith("46000"))
            str = "中國移動";
        if (this.IMSI.startsWith("46002"))
            str = "中國移動";
        if (this.IMSI.startsWith("46001"))
            str = "中國聯通";
        else if (this.IMSI.startsWith("46003"))
            str = "中國電信";
    }
    catch (Exception localException)
    {
        localException.printStackTrace();
    }
    return str;
}

別忘了宣告 TelephonyManager

使用前需先取得 TelephonyManager 的實例,透過系統服務 phone 取得即可:

this.telephonyManager =
    ((TelephonyManager) paramContext.getSystemService("phone"));

完成上述設定,就能輕鬆判斷出 SIM 卡所屬的電信業者了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved