Android 判断 SIM卡 状态 并 读取 SIM卡 内容
1.判断SIM卡状态
package com.zeph.android.sim;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import android.app.ListActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class GetSIMinfoActivity extends ListActivity {
private TelephonyManager manager;
private List
private List
private ListView mListView;
private List
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mListView = getListView();
manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
item.add("SIM卡状态");
switch (manager.getSimState()) {
case TelephonyManager.SIM_STATE_READY:
value.add("良好");
break;
case TelephonyManager.SIM_STATE_ABSENT:
value.add("无SIM卡");
break;
default:
value.add("SIM卡被锁定或未知状态");
break;
}
item.add("SIM卡序列号");
if (manager.getSimSerialNumber() != null)
value.add(manager.getSimSerialNumber());
else
value.add("无法取得");
item.add("SIM卡提供商代码");
if (manager.getSimOperator() != null)
value.add(manager.getSimOperator());
else
value.add("无法取得");
item.add("SIM卡提供商名称");
if (manager.getSimOperatorName() != null)
value.add(manager.getSimOperatorName());
else
value.add("无法取得");
item.add("SIM卡国别");
if (manager.getSimCountryIso() != null)
value.add(manager.getSimCountryIso());
else
value.add("无法取得");
Iterator
Iterator
while (itItem.hasNext() && itValue.hasNext()) {
HashMap
map.put("item", itItem.next());
map.put("value", itValue.next());
mData.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(),
mData, R.layout.item_value, new String[] { "item", "value" },
new int[] { R.id.item, R.id.value });
mListView.setAdapter(adapter);
}
}
2.读取SIM卡信息
SIM的provider是IccProvider。既然是provider,就和操作Contact的provider类似的,只是有一些微小差别。
IccProvider的Uri是content://icc/and,可以通过这个Uri来操作SIM卡。当然,使用时需要转换为android的Uri对象。android中对SIM卡操作的支持很简单,具体的查询、插入、更新和删除操作如下面的代码段
一、查询联系人
查询时,只支持获取获取联系人,即query()函数的后面几个参数都为null(其它值不起作用)。同时,也不支持类似content://icc/and/0的查询。
public void SimQuery(Activity activity) {
Uri uri = Uri.parse("content://icc/adn");
Cursor cursor = activity.getContentResolver().query(uri, null, null,
null, null);
Log.d("1023", ">>>>>>" + cursor.getCount());
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex(People._ID));
String name = cursor.getString(cursor.getColumnIndex(People.NAME));
String phoneNumber = cursor.getString(cursor
.getColumnIndex(People.NUMBER));
Log.d("1023", ">>>>>>" + "_id, " + id);
Log.d("1023", ">>>>>>" + "name, " + name);
Log.d("1023", ">>>>>>" + "phone number, " + phoneNumber);
}
}
二、插入联系人
插入联系人只要设置名字和电话号码就可以了,要注意的是姓名对应的是tag,而不是name。
public void SimInsert(Activity activity) {
Uri uri = Uri.parse("content://icc/adn");
ContentValues values = new ContentValues();
values.put("tag", "Jones");
values.put("number", "10086");
Uri newSimContactUri = activity.getContentResolver()
.insert(uri, values);
Log.d("1023",
">>>>>>" + "new sim contact uri, "
+ newSimContactUri.toString());
}
三、更新联系人
更新联系人要注意的是,它是以原先的姓名和电话号码来匹配要更新的联系人的,故要指定4个属性。
public void SimUpdate(Activity activity) {
Uri uri = Uri.parse("content://icc/adn");
ContentValues values = new ContentValues();
values.put("tag", "Jones");
values.put("number", "10086");
values.put("newTag", "Fred");
values.put("newNumber", "10000");
activity.getContentResolver().update(uri, values, null, null);
}
四、删除联系人
删除联系人,同样是以名字和电话号码来匹配的,故需要在delete的where参数中指定,下面的例子演示了删除所有SIM卡内的联系人。
public void SimDelete(Activity activity) {
Uri uri = Uri.parse("content://icc/adn");
Cursor cursor = activity.getContentResolver().query(uri, null, null,
null, null);
Log.d("1023", ">>>>>> " + cursor.getCount());
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex(People.NAME));
String phoneNumber = cursor.getString(cursor
.getColumnIndex(People.NUMBER));
String where = "tag='" + name + "'";
where += " AND number='" + phoneNumber + "'";
activity.getContentResolver().delete(uri, where, null);
}
}
上面的代码段我都测过,可以放心使用。要使上面的例子成功,则需要添加到SIM卡上的联系人有不同的名字或电话号码,若是有相同的姓名和电话号,可能会带来问题,这一点我未测过。还有,要注意的是SIM卡和U盘一样容易坏,若读写次数较多,很可能会坏掉。
3.实例
[代码] PhoneTest.java
001 | package com.android.test; |
002 | |
003 | import android.app.Activity; |
004 | import android.content.Context; |
005 | import android.content.Intent; |
006 | import android.database.Cursor; |
007 | import android.net.Uri; |
008 | import android.os.Bundle; |
009 | import android.telephony.TelephonyManager; |
010 | import android.widget.TextView; |
011 | |
012 | public class PhoneTest extends Activity { |
013 | private TextView mTextView; |
014 | protected Cursor mCursor = null ; |
015 | private TelephonyManager mTelephonyManager; |
016 | private String mString = "" ; |
017 | |
018 | /** Called when the activity is first created. */ |
019 | @Override |
020 | public void onCreate(Bundle savedInstanceState) { |
021 | super .onCreate(savedInstanceState); |
022 | setContentView(R.layout.main); |
023 | mTextView = (TextView)findViewById(R.id.text); |
024 | mTextView.setTextSize( 20 .3f); |
025 | isSimExist(); |
026 | if (getSimState() == TelephonyManager.SIM_STATE_READY){ |
027 | mString += " 卡存在\n" ; |
028 | getSimContacts( "content://icc/adn" ); //一般用这一条,如果这条不行的话可以试试下面的一条。 |
029 | getSimContacts( "content://sim/adn" );//此种读法在我们手机里不能读取,所以,还是用上个uri比较好。 |
030 | } |
031 | mTextView.setText(mString); |
032 | } |
033 | |
034 | private void getSimContacts(String str){ |
035 | Intent intent = new Intent(); |
036 | intent.setData(Uri.parse(str)); |
037 | Uri uri = intent.getData(); |
038 | mCursor = getContentResolver().query(uri, null , null , null , null ); |
039 | if (mCursor == null ){ |
040 | mString += "不能从" + str + "读数据\n" ; |
041 | return ; |
042 | } |
043 | mString += "第一列:" + mCursor.getColumnName( 0 ) + "\n" ; |
044 | mString += "第二列:" + mCursor.getColumnName( 1 ) + "\n" ; |
045 | mString += "第三列:" + mCursor.getColumnName( 2 ) + "\n" ; |
046 | mString += "第四列:" + mCursor.getColumnName( 3 ) + "\n" ; |
047 | mString += "列数:" + mCursor.getColumnCount() + "\n" ; |
048 | mString += "行数:" + mCursor.getCount() + "\n" ; |
049 | if (mCursor != null ) { |
050 | while (mCursor.moveToNext()) { |
051 | // 取得联系人名字 |
052 | int nameFieldColumnIndex = mCursor.getColumnIndex( "name" ); |
053 | mString += mCursor.getString(nameFieldColumnIndex)+ " " ; |
054 | // 取得电话号码 |
055 | int numberFieldColumnIndex = mCursor |
056 | .getColumnIndex( "number" ); |
057 | mString += mCursor.getString(numberFieldColumnIndex)+ " " ; |
058 | // 取得邮箱 |
059 | int emailsFieldColumnIndex = mCursor |
060 | .getColumnIndex( "emails" ); |
061 | mString += mCursor.getString(emailsFieldColumnIndex)+ " " ; |
062 | // 取得id |
063 | int idFieldColumnIndex = mCursor |
064 | .getColumnIndex( "_id" ); |
065 | mString += mCursor.getString(idFieldColumnIndex)+ "\n" ; |
066 | } |
067 | } |
068 | mString += mCursor + "\n" ; |
069 | mCursor.close(); |
070 | } |
071 | |
072 | private int getSimState(){ |
073 | return mTelephonyManager.getSimState(); |
074 | } |
075 | |
076 | private void isSimExist(){ |
077 | mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); |
078 | int simState = mTelephonyManager.getSimState(); |
079 | |
080 | switch (simState) { |
081 | |
082 | case TelephonyManager.SIM_STATE_ABSENT: |
083 | mString = "无卡" ; |
084 | // do something |
085 | break ; |
086 | |
087 | case TelephonyManager.SIM_STATE_NETWORK_LOCKED: |
088 | mString = "需要NetworkPIN解锁" ; |
089 | // do something |
090 | |
091 | break ; |
092 | |
093 | case TelephonyManager.SIM_STATE_PIN_REQUIRED: |
094 | mString = "需要PIN解锁" ; |
095 | // do something |
096 | break ; |
097 | |
098 | case TelephonyManager.SIM_STATE_PUK_REQUIRED: |
099 | mString = "需要PUN解锁" ; |
100 | // do something |
101 | break ; |
102 | |
103 | case TelephonyManager.SIM_STATE_READY: |
104 | mString = "良好" ; |
105 | // do something |
106 | break ; |
107 | |
108 | case TelephonyManager.SIM_STATE_UNKNOWN: |
109 | mString = "未知状态" ; |
110 | // do something |
111 | break ; |
112 | } |
113 | mTextView.setText(mString); |
114 | } |
115 | |
116 | } |
[代码] main.xml
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:orientation = "vertical" |
04 | android:layout_width = "fill_parent" |
05 | android:layout_height = "fill_parent" |
06 | > |
07 | < ScrollView android:layout_width = "fill_parent" |
08 | android:layout_height = "fill_parent" > |
09 | < LinearLayout android:orientation = "vertical" |
10 | android:layout_width = "fill_parent" |
11 | android:layout_height = "fill_parent" > |
12 | < TextView android:id = "@+id/text" |
13 | android:layout_width = "fill_parent" |
14 | android:layout_height = "wrap_content" |
15 | android:text = "@string/hello" |
16 | /> |
17 | </ LinearLayout > |
18 | </ ScrollView > |
19 | </ LinearLayout > |
[代码] AndroidManefist.xml
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < manifest xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | package = "com.android.test" |
04 | android:versionCode = "1" |
05 | android:versionName = "1.0" > |
06 | |
07 | |
08 | < application android:icon = "@drawable/icon" android:label = "@string/app_name" > |
09 | < activity android:name = ".PhoneTest" |
10 | android:label = "@string/app_name" > |
11 | < intent-filter > |
12 | < action android:name = "android.intent.action.MAIN" /> |
13 | < category android:name = "android.intent.category.LAUNCHER" /> |
14 | </ intent-filter > |
15 | </ activity > |
16 | |
17 | </ application > |
18 | < uses-permission android:name = "android.permission.READ_CONTACTS" ></ uses-permission > |
19 | </ manifest > |