赵雅智_android获取本机运营商,手机号部分能获取

时间:2023-03-09 17:05:32
赵雅智_android获取本机运营商,手机号部分能获取

手机号码不是全部的都能获取。仅仅是有一部分能够拿到。

这个是因为移动运营商没有把手机号码的数据写入到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获取本机运营商,手机号部分能获取

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhemhpMjEyOQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">赵雅智_android获取本机运营商,手机号部分能获取