admin管理员组

文章数量:1642236

Android 替换系统默认短信广播

public static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
public static final String SMS_DELIVER = "android.provider.Telephony.SMS_DELIVER";

在接受广播时,更具intent来获取subID,短信的内容等:

 Object[] pdus = (Object[]) bundle.get("pdus");
                String format = bundle.getString("format");
                Object slot = bundle.get("slot");//目前该卡的卡位
                Object phone = bundle.get("phone");
                int subscription = bundle.getInt("subscription");//subId
                LogUtil.e(TAG , "slot : " + slot + " , phone : " + phone + " , subscription : " + subscription);
                final SmsMessage[] messages = new SmsMessage[pdus.length];
                for (int i = 0; i < pdus.length; i++) {
                    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
                        messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i] , format);
                    }else{
                        messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                    }
//                    Log.e(TAG , "messages " + i + " , : " + messages[i].toString());
                }
                if(messages.length > 0){
                    String msgAddress = messages[0].getOriginatingAddress();//发件人的地址
                    long msgDate = messages[0].getTimestampMillis();//短信时间
                    for(SmsMessage message : messages){
                        msgBody.append(message.getMessageBody());//短信的内容
                    }

                }

 下面是获取卡槽位置的:

 

public class SimUtil {


    //判断是否是双卡
    public static boolean isDoubleSim(Context mContext) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            SubscriptionManager manager = SubscriptionManager.from(mContext);
            if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
            int count = manager.getActiveSubscriptionInfoCount();
            if (count == 2) {
                return true;
            }
        }
        return false;
    }

    //通过卡槽id获取sim卡的信息0代码卡槽1,1代表卡槽2
    public static String getSlotIdInfo(Context mContext, int slotId) {
        String info = "";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            SubscriptionManager manager = SubscriptionManager.from(mContext);
            if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return "";
            }
            SubscriptionInfo subInfo = manager.getActiveSubscriptionInfoForSimSlotIndex(slotId);
            if(subInfo != null){
                info = subInfo.getCarrierName().toString();
            }
        }
        return info;
    }

    //判断手机中是否装了双卡
    public static boolean isHasDoubleSim(Context mContext){
        try {
            if(isDoubleSim(mContext)){
                List<ModelSlotAndSub> slotAndSubs = getModelSlot(mContext);
                if(slotAndSubs != null && slotAndSubs.size() == 2){
                    return true;
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }


    //判断是否拥有该权限
    public static boolean IsHasPermission(Context mContext , String permission) {
        if(mContext != null){
            if(ActivityCompat.checkSelfPermission(mContext , permission)
                    != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }else{
            return false;
        }
        return true;
    }


    //申请该权限
    public static void requestOnePermission(Activity activity , String permission , int permissionCode) {
        if(activity != null){
            activity.requestPermissions(new String[]{permission} , permissionCode);
        }
    }


    //获取icc用来判断
    public static int getIcc(String iccid , List<ModelSlotAndSub> slotAndSubs){
        int icc = 0;
        if(slotAndSubs != null && slotAndSubs.size() >= 2 && !TextUtils.isEmpty(iccid)){
            if(iccid.startsWith(slotAndSubs.get(0).iccid)){
                icc = 1;
            }else if(iccid.startsWith(slotAndSubs.get(1).iccid)){
                icc = 2;
            }else{
                if(iccid.equals(slotAndSubs.get(0).subId)){
                    icc = 1;
                }else if(iccid.equals(slotAndSubs.get(1).subId)){
                    icc = 2;
                }else{
                    icc = 0;
                }
            }
        }
        return icc;
    }

    /**
     * 获取来自那张卡 即卡1 还是卡2
     * @param mContext
     * @param subID
     * @return
     */
    public static int getSlot(Context mContext , String subID){
        return getIcc(subID , getModelSlot(mContext));
    }

    //获取对应的卡槽ID和iccID 关联
    private static List<ModelSlotAndSub> getModelSlot(Context mContext) {
        List<ModelSlotAndSub> data = new ArrayList<>();
        ModelSlotAndSub modelSlotAndSub = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            SubscriptionManager subscriptionManager = SubscriptionManager.from(mContext);
            if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.READ_PHONE_STATE)
                    == PackageManager.PERMISSION_GRANTED) {
                List<SubscriptionInfo> mSubcriptionInfos = subscriptionManager.getActiveSubscriptionInfoList();
                if (mSubcriptionInfos != null) {
                    for (int i = 0; i < mSubcriptionInfos.size(); i++) {
                        SubscriptionInfo info = mSubcriptionInfos.get(i);
                        if (info != null) {
                            modelSlotAndSub = new ModelSlotAndSub();
                            modelSlotAndSub.subId = info.getSubscriptionId() + "";
                            modelSlotAndSub.simSlot = info.getSimSlotIndex() + 1 + "";
                            modelSlotAndSub.iccid = info.getIccId();
                            data.add(modelSlotAndSub);
                            //id=1, iccId=898601178[****] simSlotIndex=0
                            //id=2, iccId=898603189[****] simSlotIndex=1

                            //{id=1, iccId=898601178[****] simSlotIndex=0
                            //{id=3, iccId=898600401[****] simSlotIndex=1
                        }
                    }
                    //qb2019/07/02修改将卡1和卡2的位置旋转过来,如果第一个simSlot大于第二个的话
                    if (data != null && data.size() == 2) {
                        if (!TextUtils.isEmpty(data.get(0).simSlot) && !TextUtils.isEmpty(data.get(1).simSlot)) {
                            int simSlot1 = Integer.valueOf(data.get(0).simSlot);
                            int simSlot2 = Integer.valueOf(data.get(1).simSlot);
                            if (simSlot1 > simSlot2) {
                                ModelSlotAndSub modelSlotAndSub1 = data.get(1);
                                data.remove(data.get(1));
                                data.add(0, modelSlotAndSub1);
                            }
                        }
                    }
                }
            }
        }
        return data;
    }

}

本文标签: 短信android哪张卡槽