Android学习笔记_47_SIM卡介绍

     一、判断SIM卡属于哪个移动运营商

  1、第一种方法:获取手机的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")){ //中国电信 } }

  2、第二种方法:

TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        String operator = telManager.getSimOperator();
        if(operator!=null){
        if(operator.equals("46000") || operator.equals("46002")){
         //中国移动
        }else if(operator.equals("46001")){
         //中国联通
        }else if(operator.equals("46003")){
         //中国电信
        }

}

 

     二、从SIM卡中获取联系人信息 

   1、可以通过内容提供者进行访问,下面是主要代码。

Uri uri = Uri.parse("content://icc/adn");
String[] projection = {"_id", "name", "number"};
Cursor cursor = managedQuery(uri, projection, null, null, "name");
if(cursor!=null){
      while(cursor.moveToNext()){
           String name = cursor.getString(cursor.getColumnIndex("name"));
           String phone = cursor.getString(cursor.getColumnIndex("number"));
      }
}

/*在文件AndroidManifest.xml中添加权限

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Android系统内部通过Contentprovider对外共享Sim卡存放的联系人等信息,你可以通过操作Contentprovider来实现Sim卡信息的添删改查操作。*/

  2、完整代码如下:

package com.android.internal.telephony;

import android.content.ContentProvider;
import android.content.UriMatcher;
import android.content.ContentValues;
import com.android.internal.database.ArrayListCursor;
import android.database.Cursor;
import android.net.Uri;
import android.os.SystemProperties;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.text.TextUtils;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import com.android.internal.telephony.IccConstants;
import com.android.internal.telephony.AdnRecord;
import com.android.internal.telephony.IIccPhoneBook;

public class IccProvider extends ContentProvider {
    private static final String TAG = "IccProvider";
    private static final boolean DBG = false;

    private static final String[] ADDRESS_BOOK_COLUMN_NAMES = new String[] {
            "name", "number" };

    private static final int ADN = 1;
    private static final int FDN = 2;
    private static final int SDN = 3;

    private static final String STR_TAG = "tag";
    private static final String STR_NUMBER = "number";
    private static final String STR_PIN2 = "pin2";

    private static final UriMatcher URL_MATCHER = new UriMatcher(
            UriMatcher.NO_MATCH);

    static {
        URL_MATCHER.addURI("icc", "adn", ADN);
        URL_MATCHER.addURI("icc", "fdn", FDN);
        URL_MATCHER.addURI("icc", "sdn", SDN);
    }

    private boolean mSimulator;

    @Override
    public boolean onCreate() {
        String device = SystemProperties.get("ro.product.device");
        if (!TextUtils.isEmpty(device)) {
            mSimulator = false;
        } else {
            // simulator
            mSimulator = true;
        }
        return true;
    }

    @Override
    public Cursor query(Uri url, String[] projection, String selection,
            String[] selectionArgs, String sort) {
        ArrayList<ArrayList> results;

        if (!mSimulator) {
            switch (URL_MATCHER.match(url)) {
            case ADN:
                results = loadFromEf(IccConstants.EF_ADN);
                break;

            case FDN:
                results = loadFromEf(IccConstants.EF_FDN);
                break;

            case SDN:
                results = loadFromEf(IccConstants.EF_SDN);
                break;

            default:
                throw new IllegalArgumentException("Unknown URL " + url);
            }
        } else {
            // Fake up some data for the simulator
            results = new ArrayList<ArrayList>(4);
            ArrayList<String> contact;

            contact = new ArrayList<String>();
            contact.add("Ron Stevens/H");
            contact.add("512-555-5038");
            results.add(contact);

            contact = new ArrayList<String>();
            contact.add("Ron Stevens/M");
            contact.add("512-555-8305");
            results.add(contact);

            contact = new ArrayList<String>();
            contact.add("Melissa Owens");
            contact.add("512-555-8305");
            results.add(contact);

            contact = new ArrayList<String>();
            contact.add("Directory Assistence");
            contact.add("411");
            results.add(contact);
        }

        return new ArrayListCursor(ADDRESS_BOOK_COLUMN_NAMES, results);
    }

    @Override
    public String getType(Uri url) {
        switch (URL_MATCHER.match(url)) {
        case ADN:
        case FDN:
        case SDN:
            return "vnd.android.cursor.dir/sim-contact";

        default:
            throw new IllegalArgumentException("Unknown URL " + url);
        }
    }

    @Override
    public Uri insert(Uri url, ContentValues initialValues) {
        Uri resultUri;
        int efType;
        String pin2 = null;

        if (DBG)
            log("insert");

        int match = URL_MATCHER.match(url);
        switch (match) {
        case ADN:
            efType = IccConstants.EF_ADN;
            break;

        case FDN:
            efType = IccConstants.EF_FDN;
            pin2 = initialValues.getAsString("pin2");
            break;

        default:
            throw new UnsupportedOperationException("Cannot insert into URL: "
                    + url);
        }

        String tag = initialValues.getAsString("tag");
        String number = initialValues.getAsString("number");
        boolean success = addIccRecordToEf(efType, tag, number, pin2);

        if (!success) {
            return null;
        }

        StringBuilder buf = new StringBuilder("content://im/");
        switch (match) {
        case ADN:
            buf.append("adn/");
            break;

        case FDN:
            buf.append("fdn/");
            break;
        }

        // TODO: we need to find out the rowId for the newly added record
        buf.append(0);

        resultUri = Uri.parse(buf.toString());

        /*
         * // notify interested parties that an insertion happened
         * getContext().getContentResolver().notifyInsert( resultUri, rowID,
         * null);
         */

        return resultUri;
    }

    private String normalizeValue(String inVal) {
        int len = inVal.length();
        String retVal = inVal;

        if (inVal.charAt(0) == '\'' && inVal.charAt(len - 1) == '\'') {
            retVal = inVal.substring(1, len - 1);
        }

        return retVal;
    }

    @Override
    public int delete(Uri url, String where, String[] whereArgs) {
        int efType;

        if (DBG)
            log("delete");

        int match = URL_MATCHER.match(url);
        switch (match) {
        case ADN:
            efType = IccConstants.EF_ADN;
            break;

        case FDN:
            efType = IccConstants.EF_FDN;
            break;

        default:
            throw new UnsupportedOperationException("Cannot insert into URL: "
                    + url);
        }

        // parse where clause
        String tag = null;
        String number = null;
        String pin2 = null;

        String[] tokens = where.split("AND");
        int n = tokens.length;

        while (--n >= 0) {
            String param = tokens[n];
            if (DBG)
                log("parsing '" + param + "'");

            String[] pair = param.split("=");

            if (pair.length != 2) {
                Log.e(TAG, "resolve: bad whereClause parameter: " + param);
                continue;
            }

            String key = pair[0].trim();
            String val = pair[1].trim();

            if (STR_TAG.equals(key)) {
                tag = normalizeValue(val);
            } else if (STR_NUMBER.equals(key)) {
                number = normalizeValue(val);
            } else if (STR_PIN2.equals(key)) {
                pin2 = normalizeValue(val);
            }
        }

        if (TextUtils.isEmpty(tag)) {
            return 0;
        }

        if (efType == FDN && TextUtils.isEmpty(pin2)) {
            return 0;
        }

        boolean success = deleteIccRecordFromEf(efType, tag, number, pin2);
        if (!success) {
            return 0;
        }

        return 1;
    }

    @Override
    public int update(Uri url, ContentValues values, String where,
            String[] whereArgs) {
        int efType;
        String pin2 = null;

        if (DBG)
            log("update");

        int match = URL_MATCHER.match(url);
        switch (match) {
        case ADN:
            efType = IccConstants.EF_ADN;
            break;

        case FDN:
            efType = IccConstants.EF_FDN;
            pin2 = values.getAsString("pin2");
            break;

        default:
            throw new UnsupportedOperationException("Cannot insert into URL: "
                    + url);
        }

        String tag = values.getAsString("tag");
        String number = values.getAsString("number");
        String newTag = values.getAsString("newTag");
        String newNumber = values.getAsString("newNumber");

        boolean success = updateIccRecordInEf(efType, tag, number, newTag,
                newNumber, pin2);

        if (!success) {
            return 0;
        }

        return 1;
    }

    private ArrayList<ArrayList> loadFromEf(int efType) {
        ArrayList<ArrayList> results = new ArrayList<ArrayList>();
        List<AdnRecord> adnRecords = null;

        if (DBG)
            log("loadFromEf: efType=" + efType);

        try {
            IIccPhoneBook iccIpb = IIccPhoneBook.Stub
                    .asInterface(ServiceManager.getService("simphonebook"));
            if (iccIpb != null) {
                adnRecords = iccIpb.getAdnRecordsInEf(efType);
            }
        } catch (RemoteException ex) {
            // ignore it
        } catch (SecurityException ex) {
            if (DBG)
                log(ex.toString());
        }
        if (adnRecords != null) {
            // Load the results

            int N = adnRecords.size();
            if (DBG)
                log("adnRecords.size=" + N);
            for (int i = 0; i < N; i++) {
                loadRecord(adnRecords.get(i), results);
            }
        } else {
            // No results to load
            Log.w(TAG, "Cannot load ADN records");
            results.clear();
        }
        if (DBG)
            log("loadFromEf: return results");
        return results;
    }

    private boolean addIccRecordToEf(int efType, String name, String number,
            String pin2) {
        if (DBG)
            log("addIccRecordToEf: efType=" + efType + ", name=" + name
                    + ", number=" + number);

        boolean success = false;

        // TODO: do we need to call getAdnRecordsInEf() before calling
        // updateAdnRecordsInEfBySearch()? In any case, we will leave
        // the UI level logic to fill that prereq if necessary. But
        // hopefully, we can remove this requirement.

        try {
            IIccPhoneBook iccIpb = IIccPhoneBook.Stub
                    .asInterface(ServiceManager.getService("simphonebook"));
            if (iccIpb != null) {
                success = iccIpb.updateAdnRecordsInEfBySearch(efType, "", "",
                        name, number, pin2);
            }
        } catch (RemoteException ex) {
            // ignore it
        } catch (SecurityException ex) {
            if (DBG)
                log(ex.toString());
        }
        if (DBG)
            log("addIccRecordToEf: " + success);
        return success;
    }

    private boolean updateIccRecordInEf(int efType, String oldName,
            String oldNumber, String newName, String newNumber, String pin2) {
        if (DBG)
            log("updateIccRecordInEf: efType=" + efType + ", oldname="
                    + oldName + ", oldnumber=" + oldNumber + ", newname="
                    + newName + ", newnumber=" + newNumber);
        boolean success = false;

        try {
            IIccPhoneBook iccIpb = IIccPhoneBook.Stub
                    .asInterface(ServiceManager.getService("simphonebook"));
            if (iccIpb != null) {
                success = iccIpb.updateAdnRecordsInEfBySearch(efType, oldName,
                        oldNumber, newName, newNumber, pin2);
            }
        } catch (RemoteException ex) {
            // ignore it
        } catch (SecurityException ex) {
            if (DBG)
                log(ex.toString());
        }
        if (DBG)
            log("updateIccRecordInEf: " + success);
        return success;
    }

    private boolean deleteIccRecordFromEf(int efType, String name,
            String number, String pin2) {
        if (DBG)
            log("deleteIccRecordFromEf: efType=" + efType + ", name=" + name
                    + ", number=" + number + ", pin2=" + pin2);

        boolean success = false;

        try {
            IIccPhoneBook iccIpb = IIccPhoneBook.Stub
                    .asInterface(ServiceManager.getService("simphonebook"));
            if (iccIpb != null) {
                success = iccIpb.updateAdnRecordsInEfBySearch(efType, name,
                        number, "", "", pin2);
            }
        } catch (RemoteException ex) {
            // ignore it
        } catch (SecurityException ex) {
            if (DBG)
                log(ex.toString());
        }
        if (DBG)
            log("deleteIccRecordFromEf: " + success);
        return success;
    }

    /**
     * Loads an AdnRecord into an ArrayList. Must be called with mLock held.
     * 
     * @param record
     *            the ADN record to load from
     * @param results
     *            the array list to put the results in
     */
    private void loadRecord(AdnRecord record, ArrayList<ArrayList> results) {
        if (!record.isEmpty()) {
            ArrayList<String> contact = new ArrayList<String>(2);
            String alphaTag = record.getAlphaTag();
            String number = record.getNumber();

            if (DBG)
                log("loadRecord: " + alphaTag + ", " + number);
            contact.add(alphaTag);
            contact.add(number);
            results.add(contact);
        }
    }

    private void log(String msg) {
        Log.d(TAG, "[IccProvider] " + msg);
    }
}

 

     三、删除呼叫记录 

   1、加入权限

在文件AndroidManifest.xml中添加权限
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />

  2、设置内容提供者

负责存放呼叫记录的内容提供者源码在ContactsProvider项目下:

源码路径:com\android\providers\contacts\CallLogProvider.java
使用到的数据库在:/data/data/com.android.providers.contacts/databases/contacts2.db 表名:calls 呼叫记录有三种类型: 来电:CallLog.Calls.INCOMING_TYPE (常量值:1) 外拔:CallLog.Calls.OUTGOING_TYPE(常量值:2)

  3、删除指定号码的来电或未接呼叫记录:

IncomingCallLogContentObserver observer = new IncomingCallLogContentObserver(new Handler());
observer.setNumber("5554");
getContentResolver().registerContentObserver(CallLog.Calls.CONTENT_URI, true, observer);

private class IncomingCallLogContentObserver extends ContentObserver {
        private String number;
        public IncomingCallLogContentObserver(Handler handler){         
            super(handler);           
        }
        public void setNumber(String number){
             this.number = number;
        }
        public void onChange(boolean paramBoolean){
            ContentResolver contentResolver = getContentResolver();
            if(number!=null){              
              Uri localUri = CallLog.Calls.CONTENT_URI;
              Cursor cursor = contentResolver.query(localUri, new String[]{"_id"}, "number=? AND (type=1 OR type=3)", 
                      new String[]{number}, "_id desc limit 1");
              if(cursor.moveToFirst()){
                  contentResolver.delete(localUri, "_id=?", new String[]{cursor.getString(0)});
              }
              cursor.close();
            }
            contentResolver.unregisterContentObserver(this);
        }
}

 

 

 

 

 

 

 

posted @ 2014-01-04 16:28  若 ♂ 只如初见  阅读(694)  评论(0编辑  收藏  举报