|
我们这篇文章主要讲的就是怎么样来读取我们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;
}
}; |
|