手机上,通过*#06#可以取到手机的IMEI号,但还没有快捷的方式可以读取IMSI号码。
为此,可以写一个小Android应用程序来实现该目标。主要包括如下几个方面:
权限设置:
获取IMSI:String imsi = ((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId()
(Optional)获取IMEI:String imei = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE).getSubscriberId()
具体代码:
1. AndroidManifest.xml —— 权限
package="com.flyingbird.imsi"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="19" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name="com.flyingbird.imsi.MainActivity"
android:label="@string/app_name" >
2. fragment_main.xml —— 定义了一个TextView,用于显示IMSI号码
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.flyingbird.imsi.MainActivity$PlaceholderFragment" >
android:id="@+id/imsi_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/default_imsi_number" />
3. strings.xml —— 定义了IMSI的缺省值
Imsi
Unknown IMSI
Settings
4. MainActivity.java —— 代码部分
package com.flyingbird.imsi;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private final String TAG = "IMSI";
TextView imsi = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main/*activity_main*/);
/*if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}*/
imsi = (TextView)findViewById(R.id.imsi_number);
if (imsi == null) {
Log.d(TAG, "Can not find view id");
} else {
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
imsi.setText(telephonyManager.getDeviceId());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
这个小程序的apk下载地址:http://download.csdn.net/detail/u013344915/7714799