当前位置:操作系统 > 安卓/Android >>

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<String> item = new ArrayList<String>(); 
    private List<String> value = new ArrayList<String>();
    private ListView mListView;
    private List<Map<String, Object>> mData = new ArrayList<Map<String, Object>>();
 
    @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<String> itItem = item.iterator();
        Iterator<String> itValue = value.iterator();
        while (itItem.hasNext() && itValue.hasNext()) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            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卡信息

android读取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();
    v

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,