您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
90_获取手机sim卡的运营商
发布时间:2021-03-29 17:30:30编辑:雪饮阅读()
IMSI是用于区分蜂窝网络中不同用户的、在所有蜂窝网络中不重复的识别码。手机将IMSI存储于一个64比特的字段发送给网络。IMSI可以用来在归属位置寄存器(HLR,Home Location Register)或拜访位置寄存器(VLR,Visitor Location Register)中查询用户的信息。为了避免被监听者识别并追踪特定的用户,大部分情形下手机和网络之间的通信会使用随机产生的临时移动用户识别码(TMSI,Temporary Mobile Subscriber Identity)代替IMSI。
那么接下就来看看IMSI码如何获取,借此可以区分不同的运营商。
由于只是一个码,布局就很顺便了。activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
然后获取的逻辑也是很简单的MainActivity.java:
package com.example.getoperator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
/*
*
* IMSI共有15位,其结构如下:
MCC+MNC+MIN
MCC:Mobile Country Code,移动国家码,共3位,中国为460;
MNC:Mobile Network Code,移动网络码,共2位,联通CDMA系统使用03,一个典型的IMSI号码为460030912121001;
MIN共有10位,其结构如下:
09+M0M1M2M3+ABCD
其中的M0M1M2M3和MDN号码中的H0H1H2H3可存在对应关系,ABCD四位为自由分配。
可以看出IMSI在MIN号码前加了MCC,可以区别出每个用户的来自的国家,因此可以实现国际漫游。在同
一个国家内,如果有多个CDMA运营商,可以通过MNC来进行区别.
*
* */
String number = manager.getSubscriberId();
//imsi码为310260000000000是模拟器内部固定的
TextView tv=this.findViewById(R.id.tv);
tv.setText("IMSI码:"+number);
}
}
但是获取这个imsi码时候需要读取手机状态,所以需要清单文件上添加允许的权限AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.getoperator">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.GetOperator">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
然后部署到设备上首次部署后勾选上面的权限然后就可以打开看到获取的imsi码了。不过在这里发生了一点点小插曲,第一次部署上去后程序崩溃了,后来检查那个权限竟然是已经勾选好了的,但是还是崩溃了。然后手动去重新勾选了。这里个人分析可能原因是三个,第一个之前这个包名可能自己用过,并且也有依赖这个权限,所以产生错乱了吧。第二个可能部署到设备上时候运行我先部署过模拟器导致产生的错乱(自己都在笑)。第三个可能是部署到设备上时候运行应用过快,运行时候对权限这个地方出现了逻辑混乱(好像和第一个有什么联系一样)。无论问题如何,这里暂且先不探索了。则最后部署设备上的效果如
我的设备是魅族16t,然后是双卡的,但是最后使用的是联通卡,当前直接连接的是wifi,关闭了数据流量。
关键字词:android,sim,imsi,运营商
上一篇:89_如何安全的退出应用程序