我们这篇文章主要讲的就是怎么样来读取我们sim里的所有信息,这个其实对我们也是很重要的,下面我们就来看看怎么样用代码来获取吧。
主类:SimData.java
java代码:
public class SimData extends ListActivity { private TelephonyManager telMgr; private List<String> item=new ArrayList<String>(); private List<String> value=new ArrayList<String>(); @SuppressWarnings(“static-access”) @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* 载入main.xml Layout */ setContentView(R.layout.main); //创建电话管理与手机建立连接 telMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); /* 将取得的信息写入List中 */ /* 取得SIM卡状态 */ item.add(getResources().getText(R.string.str_list0).toString()); if(telMgr.getSimState()==telMgr.SIM_STATE_READY){ value.add(“良好”); }else if(telMgr.getSimState()==telMgr.SIM_STATE_ABSENT){ value.add(“无SIM卡”); }else if(telMgr.getSimState() == telMgr.SIM_STATE_NETWORK_LOCKED){ value.add(“需要NetWork PIN 解锁”); }else if(telMgr.getSimState() == telMgr.SIM_STATE_PIN_REQUIRED){ value.add(“需要SIM卡的PIN解锁”); }else if(telMgr.getSimState() == telMgr.SIM_STATE_PUK_REQUIRED){ value.add(“需要SIM卡的PUK解锁”); }else if(telMgr.getSimState() == telMgr.SIM_STATE_UNKNOWN){ value.add(“SIM卡状态未知”); } /* 取得SIM卡卡号/IMEI:手机串号 */ item.add(getResources().getText(R.string.str_list1).toString()); if(telMgr.getSimSerialNumber()!=null){ value.add(telMgr.getSimSerialNumber()); }else{ value.add(“无法取得”); } /* 取得SIM卡卡号/IMSI:国际移动用户识别码*/ item.add(getResources().getText(R.string.str_list2).toString()); if(telMgr.getSubscriberId()!=null){ value.add(telMgr.getSubscriberId()); }else{ value.add(“无法取得”); } /* 取得SIM卡供货商代码 */ item.add(getResources().getText(R.string.str_list3).toString()); if(telMgr.getSimOperator().equals(“”)){ value.add(“无法取得”); }else{ value.add(telMgr.getSimOperator()); } /* 取得SIM卡供货商名称 */ item.add(getResources().getText(R.string.str_list4).toString()); if(telMgr.getSimOperatorName().equals(“”)){ value.add(“无法取得”); }else{ value.add(telMgr.getSimOperatorName()); } /* 取得SIM卡国别 */ item.add(getResources().getText(R.string.str_list5).toString()); if(telMgr.getSimCountryIso().equals(“”)){ value.add(“无法取得”); }else{ value.add(telMgr.getSimCountryIso()); }
复制代码
java代码:
/*取得SIM卡的网络类型*/ item.add(getResources().getText(R.string.str_list6).toString()); if(telMgr.getPhoneType() == telMgr.PHONE_TYPE_CDMA){ value.add(“SIM的类型是CDMA”); }else if(telMgr.getPhoneType() == telMgr.PHONE_TYPE_GSM){ value.add(“SIM的类型是GSM”); }else if(telMgr.getPhoneType() == telMgr.PHONE_TYPE_NONE){ value.add(“SIM的类型是NONE”); }else{ value.add(“无法取得”); } /*取得SIM卡驱动的ID号*/ item.add(getResources().getText(R.string.str_list7).toString()); if(telMgr.getDeviceId().equals(“”)){ value.add(“无法取得”); }else{ value.add(telMgr.getDeviceId()); } /*取得SIM卡号码/手机号码*/ item.add(getResources().getText(R.string.str_list8).toString()); if(telMgr.getLine1Number().equals(“”)){ value.add(“无法取得”); }else{ value.add(telMgr.getLine1Number()); } /*取得SIM卡CDMA基站的识别ID*/ item.add(getResources().getText(R.string.str_list9).toString()); if(telMgr.getCellLocation() == null){ value.add(“无法取得”); }else{ value.add(String.valueOf(((CdmaCellLocation) telMgr.getCellLocation()).getBaseStationId())); } /*取得SIM卡CDMA基站的地理坐标*/ item.add(getResources().getText(R.string.str_list10).toString()); if(telMgr.getCellLocation() == null){ value.add(“无法取得”); }else{ value.add(“经度:” +String.valueOf(((CdmaCellLocation) telMgr.getCellLocation()).getBaseStationLongitude()) +” “+“纬度:” + String.valueOf(((CdmaCellLocation) telMgr.getCellLocation()).getBaseStationLatitude())); } /* 使用自定义的MyAdapter来将数据传入ListActivity */ setListAdapter(new MyAdapter(this,item,value)); } }
复制代码
/* * Wapdroid - Android Location based Wifi Manager * Copyright (C) 2009 Bryan Emmanuel * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * * Bryan Emmanuel piusvelte@gmail.com */ package eoe.demo; import java.util.List; import android.app.AlarmManager; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.net.NetworkInfo; import android.net.wifi.WifiManager; import android.os.BatteryManager; import android.os.IBinder; import android.os.RemoteException; import android.telephony.CellLocation; import android.telephony.NeighboringCellInfo; import android.telephony.PhoneStateListener; import android.telephony.SignalStrength; import android.telephony.TelephonyManager; import android.telephony.gsm.GsmCellLocation; import android.util.Log; public class WapdroidService extends Service { private static int NOTIFY_ID = 1; public static final String WAKE_SERVICE = "com.piusvelte.wapdroid.WAKE_SERVICE"; private WapdroidDbAdapter mDbHelper; private NotificationManager mNotificationManager; private TelephonyManager mTeleManager; private String mSsid = "", mBssid = "", mOperator = "", mOperatorName = "", mMcc = ""; private List<NeighboringCellInfo> mNeighboringCells; private WifiManager mWifiManager; private int mCid = WapdroidDbAdapter.UNKNOWN_CID, mLac = WapdroidDbAdapter.UNKNOWN_CID, mRssi = 99, mWifiState, mInterval, mPhoneType = TelephonyManager.PHONE_TYPE_NONE, mNetworkType = TelephonyManager.NETWORK_TYPE_UNKNOWN; private boolean mWifiIsEnabled, mNotify, mVibrate, mLed, mRingtone, mBatteryOverride, mBatteryLock = true; private AlarmManager mAlarmMgr; private PendingIntent mPendingIntent; private IWapdroidUI mWapdroidUI; private boolean mControlWifi = true; private static final String TAG = "Wapdroid"; private double mBatteryPercentage = 100.0, mBatteryRemaining; private final IWapdroidService.Stub mWapdroidService = new IWapdroidService.Stub() { public void updatePreferences(int interval, boolean notify, boolean vibrate, boolean led, boolean ringtone, boolean batteryOverride, int batteryPercentage) throws RemoteException { mInterval = interval; if (mNotify && !notify) { mNotificationManager.cancel(NOTIFY_ID); mNotificationManager = null;} else if (!mNotify && notify) { mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); CharSequence contentTitle = getString(mWifiIsEnabled ? R.string.label_enabled : R.string.label_disabled); Notification notification = new Notification((mWifiIsEnabled ? R.drawable.scanning : R.drawable.status), contentTitle, System.currentTimeMillis()); PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(), 0, new Intent(getBaseContext(), WapdroidService.class), 0); notification.setLatestEventInfo(getBaseContext(), contentTitle, getString(R.string.app_name), contentIntent); mNotificationManager.notify(NOTIFY_ID, notification);} mNotify = notify; mVibrate = vibrate; mLed = led; mRingtone = ringtone; mBatteryOverride = batteryOverride; mBatteryPercentage = batteryPercentage;} public void setCallback(IBinder mWapdroidUIBinder) throws RemoteException { if (mWapdroidUIBinder != null) { Log.v(TAG, "setCallback, enable wifi control"); mControlWifi = true; mWapdroidUI = IWapdroidUI.Stub.asInterface(mWapdroidUIBinder); if ((mWapdroidUI != null) && (mCid != WapdroidDbAdapter.UNKNOWN_CID)) { try { String cells = "'" + Integer.toString(mCid) + "'"; if (!mNeighboringCells.isEmpty()) { for (NeighboringCellInfo n : mNeighboringCells) cells += ",'" + Integer.toString(n.getCid()) + "'";} mWapdroidUI.setOperator(mOperatorName, mMcc, mOperator); mWapdroidUI.setCellInfo(Integer.toString(mCid), Integer.toString(mLac)); mWapdroidUI.setWifiInfo(mWifiState, mSsid, mBssid); mWapdroidUI.setSignalStrength(mRssi); mWapdroidUI.setCells(cells); mWapdroidUI.setBattery(mBatteryRemaining);} catch (RemoteException e) {}}}} public void suspendWifiControl() throws RemoteException { Log.v(TAG, "running wifi settings, disable wifi control"); mControlWifi = false; } };
复制代码
Android 获取MIEI ISMI Sim卡串号等等信息
TelephonyManager telephonemanage = (TelephonyManager) getWindow() .getContext().getSystemService(Context.TELEPHONY_SERVICE); try { edit1.setText("MIEI: " + telephonemanage.getDeviceId() + "\n SimSSN " + telephonemanage.getSimSerialNumber() + "\n IMSI " + telephonemanage.getSubscriberId()); } catch (Exception e) { edit1.setText(e.getMessage()); }
复制代码
如果要获取手机号识别,即表示哪个国家哪个网络的号码,完全不用使用IMSI号码,而直接 使用此即可:
telephonemanage.getSimOperator(); 得出的即是识别码
此在2G使用的GSM网络里面没有问题,但是在3G卡上或者是CDMA卡上没有进行相应的测试!无法得出结果!
读取SIM卡的状态理论说明
最近要用到读取联系人的功能,那么联系人保存在两个地方,一个是SIM卡,就是我们的手机卡;另一个就是手机本地数据库中保存的联系人信息。大家可以拿出自己的手机,选定某一个联系人,点击功能菜单,你会发现有一个选项是同步到SIM卡或者手机,这样就是说:你可以把联系人只保存在SIM卡上,或者只保存在手机本地数据库中,但是这样,当你的手机没电了,你想把卡放在别人手机上的时候,你的电话号码就很会没有你原来手机本地保存的那些联系人。你当然可以同时保存在SIM卡上和手机本地数据库中。 OK,由此引申出两个重要的地方,第一:如何读取SIM卡的状态,如果读写SIM卡;第二:如何读写手机本地数据库中的联系人数据。我想,这是很基础的功能,任何一个人都要掌握的。我们在模拟器上可以添加联系人,但是请务必注意,这只是保存在手机本地数据库中的联系人,模拟器上是没有SIM卡的,因此,如果要读写SIM卡,就要用真机测试,但是苦于我有一个诺基亚的智能手机了,我又不舍得再买一个Android的手机做测试,因为最便宜的都得一千多,没办法,关于SIM卡的,我没办法测试。我只对手机本地数据库中的联系人做了测试。 好了,我们回到正题。第一步,学会判断SIM卡的状态及其它的信息。可以参看示例代码。 TelephonyManager的应用[取得SIM卡的状态] 手机的最主要功用就是打电话,如果没有电信公司提供的SIM卡,就不能正常地拨打电话,那么,我们有什么方法可以取得SIM卡的状态及相关资料呢? Android API中的TelephonyManager(Android.telephony.TelephonyManager)对象,提供了几个方法可以快速取得SIM卡的状态及相关信息。 程序中以getSystemService(TELEPHONY_SERVICE)来取得TelephonyManager对象,以TelephonyManager提供的方法来取得SIM卡状态及相关信息,将取得的信息存入自定义的MyAdapter中,最后以setListAdapter()将MyAdpter内的信息显示于ListView中。自定义的Adapter对象,以row_layout.xml作为Layout,程序中依照Layout的设置值来显示信息名称及信息内容。 AndroidManifest.xml AndroidManifest.xml本身必须要在Activity里设置读取电话状态的权限(Android.permission.READ_ PHONE_STATE)。 在取得SIM卡的相关信息时,有时会因某些情况而取不到值。例如,手机没有插入SIM卡,这时候的返回值可能为null或空白字符串,所以在程序中添加空白与null的检查会是比较保险的做法,以免因此造成系统错误。 程序以TelephonyManager.getSimState()来取得SIM卡的状态,可能出现的返回状态如表 效果图:
Android 获取sim卡运营商信息 TelephonyManager tm = (TelephonyManager)Context.getSystemService(Context.TELEPHONY_SERVICE); TelephonyManager 的使用 TelephonyManager 提供设备上获取通讯服务信息的入口,应用程序使用这个类的方法来获取电话的服务商或者状态。程序也可以注册一个监听器来监听电话状态的改变。不需要直接实例化这个类,使用Context.getSystemService(Context.TELEPHONY_SERVICE)来获取这个类的实例。 注意:一些电话信息需要相应的权限。方法无效 getSimOperatorName() Returns the Service Provider Name (SPN). // 获取服务提供商名字,比如电信,联通,移动用下面的方法。 第一种方法: 获取手机的IMSI码,并判断是中国移动\中国联通\中国电信 TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); /** 获取SIM卡的IMSI码 * SIM卡唯一标识:IMSI 国际移动用户识别码(IMSI:International Mobile Subscriber Identification Number)是区别移动用户的标志, * 储存在SIM卡中,可用于区别移动用户的有效信息。 IMSI由MCC、MNC、MSIN组成,其中MCC为移动国家号码,由3位数字组成, * 唯一地识别移动客户所属的国家,我国为460;MNC为网络id,由2位数字组成, * 用于识别移动客户所归属的移动网络,中国移动为00,中国联通为01,中国电信为03; MSIN为移动客户识别码,采用等长11位数字构成。 * 唯一地识别国内GSM移动通信网中移动客户。所以要区分是移动还是联通,只需取得SIM卡中的MNC字段即可 */ String imsi = telManager.getSubscriberId(); if(imsi!=null){ if(imsi.startsWith(“46000″) || imsi.startsWith(“46002″)) {//因为移动网络编号46000下的IMSI已经用完,所以虚拟了一个46002编号,134/159号段使用了此编号 //中国移动 }else if(imsi.startsWith(“46001″)){ //中国联通 }else if(imsi.startsWith(“46003″)){ //中国电信 } } 第二种方法 TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); String operator = telManager.getSimOperator(); if(operator!=null){ if(operator.equals(“46000″) || operator.equals(“46002″)|| operator.equals(“46007″)){ //中国移动 }else if(operator.equals(“46001″)){ //中国联通 }else if(operator.equals(“46003″)){ //中国电信 } } 在文件AndroidManifest.xml中添加权限其他方法具体使用方法请查看API 文档TelephonyManager中方法说明。 在文件 AndroidManifest.xml 中添加权限 <uses-permission android:name= "android.permission.READ_PHONE_STATE"/> TelephonyManager tel = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); String simOperator = tel.getSimOperator(); IMSI共有15位,其结构如下: MCC+MNC+MIN MCC:Mobile Country Code,移动国家码,共3位,中国为460; MNC:Mobile Network Code,移动网络码,共2位,电信03,移动02,联通GSM 01,一个典型的IMSI号码为460030912121001; MIN共有10位,其结构如下: 09+M0M1M2M3+ABCD 其中的M0M1M2M3和MDN号码中的H0H1H2H3可存在对应关系,ABCD四位为自由分配。可以看出IMSI在MIN号码前加了MCC,可以区别出每个用户的来自的国家,因此可以实现国际漫游。在同一个国家内,如果有多个CDMA运营商,可以通过MNC来进行区别. 功能 说明 getCellLocation() 返回的单元格位置的装置 ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION getDeviceId() 返回的IMEI / MEID的设备。 如果该设备是GSM设备然后IMEI号将被退回,如果该设备是一个CDMA设备然后MEID 将被退回 READ_PHONE_STATE getLine1Number() 返回设备的电话号码(MSISDN号码) READ_PHONE_STATE getNetworkOperatorName() 返回注册的网络运营商的名字 getNetworkOperator() 返回的MCC +跨国公司的注册网络运营商 getNetworkCountryIso() 返回注册的网络运营商的国家代码 getSimCountryIso() 返回SIM卡运营商的国家代码 READ_PHONE_STATE getSimOperator() 返回SIM卡运营商的单个核细胞数+冶 READ_PHONE_STATE getSimOperatorName() 返回SIM卡运营商的名字 READ_PHONE_STATE getSimSerialNumber() 返回SIM卡的序列号 READ_PHONE_STATE getNetworkType() 返回网络设备可用的类型。 这将是下列其中一个值: TelephonyManager.NETWORK_TYPE_UNKNOWN TelephonyManager.NETWORK_TYPE_GPRS TelephonyManager.NETWORK_TYPE_EDGE TelephonyManager.NETWORK_TYPE_UMTS READ_PHONE_STATE getPhoneType() 返回设备的类型。 这将是以下值之一: TelephonyManager.PHONE_TYPE_NONE TelephonyManager.PHONE_TYPE_GSM TelephonyManager.PHONE_TYPE_CDMA READ_PHONE_STATE getSubscriberId() 返回用户识别码(的IMSI)的设备 READ_PHONE_STATE getNeighboringCellInfo() 返回NeighboringCellInfo类代表名单相邻小区的信息,如果可用,否则将返回null ACCESS_COARSE_UPDATES 下面是示例代码: ContactTest2.rar (44.89 KB, 下载次数: 39)
获取手机设备的IMSI / IMEI 信息
IMSI 全称为 International Mobile Subscriber Identity,中文翻译为国际移动用户识别码。它是在公众陆地移动电话网(PLMN)中用于唯一识别移动用户的一个号码。在GSM网络,这个号码通常被存放在SIM卡中。 IMEI 全称为 International Mobile Equipment Identity,中文翻译为国际移动装备辨识码, 即通常所说的手机序列号,用于在手机网络中识别每一部独立的手机,是国际上公认的手机标志序号,相当于移动电话的身份证。序列号共有15位数字,前6位(TAC)是型号核准号码,代表手机类型。接着2位(FAC)是最后装配号,代表产地。后6位(SNR)是串号,代表生产顺序号。最后1位(SP)一般为 0,是检验码,备用。国际移动装备辨识码一般贴于机身背面与外包装上,同时也存在于手机记忆体中,通过输入*#06#即可查询。 Android中获取IMSI和IMEI的代码很简单,如下:
TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); String imsi = mTelephonyMgr.getSubscriberId(); String imei = mTelephonyMgr.getDeviceId(); Log.i("IMSI", imsi); Log.i("IMEI", imei);
复制代码
另外不要忘了在AndroidManifest.xml中加上读取手机状态的权限
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
复制代码
如何获取android手机信息
Android开发 平台中,可通过TelephonyManager 获取本机号码。 java代码: [mw_shl_code=java,true]TelephonyManager phoneMgr=(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE); txtPhoneNumber.setText(phoneMgr.getLine1Number()); //txtPhoneNumber是一个EditText 用于显示手机号[/mw_shl_code] 注:根据Android的安全机制,在使用TelephonyManager时,必须在AndroidManifest.xml中添加<uses-permission android:name="READ_PHONE_STATE" /> 否则无法获得系统的许可。 手机型号 Build.MODEL java代码: [mw_shl_code=java,true]private void loadPhoneStatus(){ TelephonyManager phoneMgr=(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE); txtPhoneModel.setText(Build.MODEL); //手机型号 txtPhoneNumber.setText(phoneMgr.getLine1Number());//本机电话号码 txtSdkVersion.setText(Build.VERSION.SDK);//SDK版本号 txtOsVersion.setText(Build.VERSION.RELEASE);//Firmware/OS 版本号 }[/mw_shl_code] 事实上,Build能向我们提供包括 硬件厂商,硬件编号,序列号等很多信息 调用方法也都同上,很简单。 java代码: [mw_shl_code=java,true]String BOARD The name of the underlying board, like "goldfish". String BOOTLOADER The system bootloader version number. String BRAND The brand (e.g., carrier) the software is customized for, if any. String CPU_ABI The name of the instruction set (CPU type + ABI convention) of native code. String CPU_ABI2 The name of the second instruction set (CPU type + ABI convention) of native code. String DEVICE The name of the industrial design. String DISPLAY A build ID string meant for displaying to the user String FINGERPRINT A string that uniquely identifies this build. String HARDWARE The name of the hardware (from the kernel command line or /proc). String HOST String ID Either a changelist number, or a label like "M4-rc20". String MANUFACTURER The manufacturer of the product/hardware. String MODEL The end-user-visible name for the end product. String PRODUCT The name of the overall product. String RADIO The radio firmware version number. String SERIAL A hardware serial number, if available. String TAGS Comma-separated tags describing the build, like "unsigned,debug". long TIME String TYPE The type of build, like "user" or "eng". String UNKNOWN Value used for when a build property is unknown. String USER[/mw_shl_code]