获取手机号码信息以及imsi信息,需要的权限为
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
package com.phoneinfotest;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textMobile;
private TextView textImsi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(LinearLayout.VERTICAL);
Button button = new Button(this);
button.setText("获取手机信息");
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
textMobile.setText(telephonyManager.getLine1Number());
textImsi.setText(telephonyManager.getSubscriberId());
}
});
LinearLayout mobileLayout = new LinearLayout(this);
mobileLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
mobileLayout.setOrientation(LinearLayout.HORIZONTAL);
TextView labelMobile = new TextView(this);
labelMobile.setText("手机号码:");
textMobile = new TextView(this);
mobileLayout.addView(labelMobile);
mobileLayout.addView(textMobile);
/**
* IMSI共有15位,其结构如下:
* MCC+MNC+MSIN
* MCC:Mobile Country Code,移动国家码,MCC的资源由国际电联(ITU)统一分配和管理,唯一识别移动用户所属的国家,共3位,中国为460;
* MNC:Mobile Network Code,移动网络码,2~3位,中国移动系统使用00、02、07,中国联通GSM系统使用01、06,中国电信CDMA系统使用03、05,中国铁通系统使用20,一个典型的IMSI号码为460030912121001;
*/
LinearLayout imsiLayout = new LinearLayout(this);
imsiLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
imsiLayout.setOrientation(LinearLayout.HORIZONTAL);
TextView labelImsi = new TextView(this);
labelImsi.setText("imsi:");
textImsi = new TextView(this);
imsiLayout.addView(labelImsi);
imsiLayout.addView(textImsi);
linearLayout.addView(button);
linearLayout.addView(mobileLayout);
linearLayout.addView(imsiLayout);
setContentView(linearLayout);
}
}