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> |