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

Android 獲取手機號碼與運營商信息的完整實現

編輯:關於 Android 程式設計

讀取 SIM 卡信息在 Android 裡通過 TelephonyManager 完成。這個類是訪問設備通訊能力的總入口,能拿到運營商名稱、國家碼、網絡類型等一系列信息。

封裝 SIM 卡信息類

package com.pei.activity;

import android.content.Context;
import android.telephony.TelephonyManager;

/**
 * 讀取 SIM 卡信息
 * 注意:需要在 manifest 中申請 READ_PHONE_STATE 權限
 */
public class SIMCardInfo {

    /**
     * TelephonyManager 提供設備上獲取通訊服務信息的入口,
     * 可用於判斷電信服務商、國家以及各類用戶訪問信息。
     * 不需要直接 new,通過 Context.getSystemService 獲取實例。
     */
    private TelephonyManager telephonyManager;

    /** 國際移動用戶識別碼 */
    private String IMSI;

    public SIMCardInfo(Context context) {
        telephonyManager = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
    }

    /** 獲取本機號碼 */
    public String getNativePhoneNumber() {
        return telephonyManager.getLine1Number();
    }

    /**
     * 獲取運營商名稱
     * 需要權限:android.permission.READ_PHONE_STATE
     */
    public String getProvidersName() {
        String ProvidersName = null;
        // 返回 IMSI,即這張卡的唯一標識
        IMSI = telephonyManager.getSubscriberId();
        // IMSI 前 3 位 460 為中國國家碼,
        // 緊隨的 2 位中 00/02 為中國移動,01 為中國聯通,03 為中國電信
        if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
            ProvidersName = "中國移動";
        } else if (IMSI.startsWith("46001")) {
            ProvidersName = "中國聯通";
        } else if (IMSI.startsWith("46003")) {
            ProvidersName = "中國電信";
        }
        return ProvidersName;
    }
}

界面與調用

package com.pei.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class AndroidUtilActivity extends Activity {
    private Button button_getSIMInfo;
    private TextView number;
    private TextView privoid;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button_getSIMInfo = (Button) this.findViewById(R.id.getSIMInfo);
        number = (TextView) this.findViewById(R.id.textView1);
        privoid = (TextView) this.findViewById(R.id.textView2);
        button_getSIMInfo.setOnClickListener(new ButtonListener());
    }

    class ButtonListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            if (v == button_getSIMInfo) {
                SIMCardInfo siminfo = new SIMCardInfo(AndroidUtilActivity.this);
                number.setText(siminfo.getNativePhoneNumber());
                privoid.setText(siminfo.getProvidersName());
            }
        }
    }
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <Button
        android:id="@+id/getSIMInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="獲取手機號" />

</LinearLayout>

實戰中的幾個現實問題

權限是硬門檻。 READ_PHONE_STATE 屬於危險權限,Android 6.0 之後必須運行時動態申請,僅在 manifest 裡聲明是拿不到數據的。Android 10 起,getLine1Number() 與 getSubscriberId() 更是被進一步限制,普通應用基本無法再讀取號碼和 IMSI,需要系統或電信運營商級別的權限。

號碼可能是空的。 getLine1Number() 依賴運營商是否把號碼寫入 SIM 卡,不少運營商並不寫,因此返回 null 或空串是常見情況,代碼裡務必做空值判斷,不要默認一定能拿到。

雙卡與運營商演進。 上面用 IMSI 前綴判斷運營商的做法在早期是有效的,但如今中國移動、聯通、電信各有大量號段,硬編碼前綴列表容易漏判。更穩妥的方式是直接用 TelephonyManager.getNetworkOperatorName() 讓系統告訴你運營商名稱。雙卡場景下則需要通過 SubscriptionManager 逐卡處理。

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