1. 程式人生 > >趙雅智_android獲取本機運營商,手機號部分能獲取

趙雅智_android獲取本機運營商,手機號部分能獲取

全部 電信 ret parent ack imsi new fonts 獲取手機號

手機號碼不是全部的都能獲取。僅僅是有一部分能夠拿到。

這個是因為移動運營商沒有把手機號碼的數據寫入到sim卡中.SIM卡僅僅有唯一的編號。供網絡與設備 識別那就是IMSI號碼,手機的信號也能夠說是通過這個號碼在網絡中傳遞的,並非手機號碼。

試想。你的SIM丟失後,補辦一張新的會換號碼嗎?

是不會 的.就是由於在你的手機號碼相應的IMSI號 在移動運營商中被改動成新SIM卡的IMSI號碼。

那麽手機號為什麽有的就能顯示呢?

這個就像是一個變量,當移動運營商為它賦值了,它自然就會有值。不賦值自然為空。


國內sim卡存的是imsi號,沒有電話號碼存在機器上。僅僅有當你和基站發生聯系後。由運營商給你分配號碼。


可是我也寫了一個類實現獲取運營商

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

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

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

    <Button
        android:id="@+id/getSIMInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="獲取手機號碼等信息" >
    </Button>

</LinearLayout>


SIMCardInfo.java:

/**
 * @author yazhizhao
 * 20142014-10-22上午9:13:04
 */
package com.example.android_sms;

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

/**
 * @author yazhizhao 20142014-10-22上午9:13:04
 */
public class SIMCardInfo {
	/**
	 * TelephonyManager提供設備上獲取通訊服務信息的入口。 應用程序能夠使用這個類方法確定的電信服務商和國家 以及某些類型的用戶訪問信息。
	 * 應用程序也能夠註冊一個監聽器到電話收狀態的變化。

不須要直接實例化這個類 * 使用Context.getSystemService(Context.TELEPHONY_SERVICE)來獲取這個類的實例。 */ private TelephonyManager telephonyManager; private String IMSI;// 國際移動用戶識別碼 public SIMCardInfo(Context context) { telephonyManager = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); } /** * 獲取當前設置的電話號碼 * * @author yazhizhao 20142014-10-22上午9:44:19 * @return */ public String getNativePhoneNumber() { String NativePhoneNumber = null; NativePhoneNumber = telephonyManager.getLine1Number(); return NativePhoneNumber; } /** * 獲取手機服務商信息 須要增加權限<uses-permission * android:name="android.permission.READ_PHONE_STATE"/> * * @author yazhizhao 20142014-10-22上午9:44:31 * @return */ public String getProvidersName() { String ProvidersName = null; // 返回唯一的用戶ID;就是這張卡的編號神馬的 IMSI = telephonyManager.getSubscriberId(); // IMSI號前面3位460是國家,緊接著後面2位00 02是中國移動。01是中國聯通,03是中國電信。 Log.i("MainActivity", "IMSI" + IMSI); if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) { ProvidersName = "中國移動"; } else if (IMSI.startsWith("46001")) { ProvidersName = "中國聯通"; } else if (IMSI.startsWith("46003")) { ProvidersName = "中國電信"; } return ProvidersName; } }


MainActivity.java

package com.example.android_sms;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

/**
 * 註意權限 <uses-permission android:name="android.permission.READ_PHONE_STATE" />
 * 
 * @author yazhizhao 20142014-10-22上午9:18:04
 */
public class MainActivity extends Activity {
	private TextView number;
	private TextView privoid;
	private String TAG = "MainActivity";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		number = (TextView) this.findViewById(R.id.tv_num);
		privoid = (TextView) this.findViewById(R.id.tv_privoid);
	}

	public void click(View v) {

		SIMCardInfo siminfo = new SIMCardInfo(MainActivity.this);
		Log.i(TAG, "運營商" + siminfo.getProvidersName());
		Log.i(TAG, "電話" + siminfo.getNativePhoneNumber());
		number.setText(siminfo.getNativePhoneNumber());
		privoid.setText(siminfo.getProvidersName());

	}

}

執行效果:

技術分享技術分享



趙雅智_android獲取本機運營商,手機號部分能獲取