android安卓APP获取手机设备信息和手机号码的代码示例
下面我从安卓开发的角度,简单写一下如何获取手机设备信息和手机号码准备条件:一部安卓手机、手机SIM卡确保插入手机里、eclipse ADT和android-sdk开发环境
第一步:新建一个android工程(JinshanTest),
并需要在工程的AndroidManifest.xml文件中,添加权限
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
图例:
第二步:新建一个工具类PhoneInfo.java
[java]
package com.jinshan.test;
import android.content.Context;
import android.telephony.TelephonyManager;
/**
* 读取手机设备信息测试代码
* http://www.souapp.com 搜应用网
* song2c@163.com
* 宋立波
*/
public class PhoneInfo {
private TelephonyManager telephonyManager;
/**
* 国际移动用户识别码
*/
private String IMSI;
private Context cxt;
public PhoneInfo(Context context) {
cxt=context;
telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
}
/**
* 获取电话号码
*/
public String getNativePhoneNumber() {
String NativePhoneNumber=null;
NativePhoneNumber=telephonyManager.getLine1Number();
return NativePhoneNumber;
}
/**
* 获取手机服务商信息
*/
public String getProvidersName() {
String ProvidersName = "N/A";
try{
IMSI = telephonyManager.getSubscriberId();
// IMSI号前面3位460是国家,紧接着后面2位00 02是中国移动,01是中国联通,03是中国电信。
System.out.println(IMSI);
if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
ProvidersName = "中国移动";
} else if (IMSI.startsWith("46001")) {
ProvidersName = "中国联通";
} else if (IMSI.startsWith("46003")) {
ProvidersName = "中国电信";
}
}catch(Exception e){
e.printStackTrace();
}
return ProvidersName;
}
public String getPhoneInfo(){
TelephonyManager tm = (TelephonyManager)cxt.getSystemService(Context.TELEPHONY_SERVICE);
StringBuilder sb = new StringBuilder();
sb.append("\nDeviceId(IMEI) = " + tm.getDeviceId());
sb.append("\nDeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion());
sb.append("\nLine1Number = " + tm.getLine1Number());
sb.append("\nNetworkCountryIso = " + tm.getNetworkCountryIso());
sb.append("\nNetworkOperator = " + tm.getNetworkOperator());
sb.append("\nNetworkOperatorName = " + tm.getNetworkOperatorName());
sb.append("\nNetworkType = " + tm.getNetworkType());
sb.append("\nPhoneType = " + tm.getPhoneType());
sb.append("\nSimCountryIso = " + tm.getSimCountryIso());
sb.append("\nSimOperator = " + tm.getSimOperator());
sb.append("\nSimOperatorName = " + tm.getSimOperatorName());
sb.append("\nSimSerialNumber = " + tm.getSimSerialNumber());
sb.append("\nSimState = " + tm.getSimState());
sb.append("\nSubscriberId(IMSI) = " + tm.getSubscriberId());
sb.append("\nVoiceMailNumber = " + tm.getVoiceMailNumber());
return sb.toString();
}
}
第三步:在启动的Activity,JinshanTestActivity中添加,调用代码:
[java]
package com.jinshan.test;
import android.app.Activity;
import android.os.Bundle;
public class JinshanTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PhoneInfo siminfo = new PhoneInfo(JinshanTestActivity.this);
System.out.println("getProvidersName:"+siminfo.getProvidersName());
System.out.println("getNativePhoneNumber:"+siminfo.getNativePhoneNumber());
System.out.println("---------------
补充:移动开发 , Android ,