Dual Sim Android

前端 未结 4 1902
春和景丽
春和景丽 2021-02-20 09:58

Is there a way that I can access the other sim in my android? I have an android Cherry Mobile Orbit which is a dual sim android phone. I want to develop an SMS application for m

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-20 10:47

    This method can be used to select one SIM from 2 SIMs!

    //above Android API 22
    if (Build.VERSION.SDK_INT > 22) {
        //for dual sim mobile
        SubscriptionManager localSubscriptionManager = SubscriptionManager.from(this);
    
        if (localSubscriptionManager.getActiveSubscriptionInfoCount() > 1) {
         //if there are two sims in dual sim mobile
            List localList = localSubscriptionManager.getActiveSubscriptionInfoList();
            SubscriptionInfo simInfo = (SubscriptionInfo) localList.get(0);
            SubscriptionInfo simInfo1 = (SubscriptionInfo) localList.get(1);
    
            final String sim1 = simInfo.getDisplayName().toString();
            final String sim2 = simInfo1.getDisplayName().toString();
    
        }else{
         //if there is 1 sim in dual sim mobile
            TelephonyManager tManager = (TelephonyManager) getBaseContext()
                    .getSystemService(Context.TELEPHONY_SERVICE);
    
            String sim1 = tManager.getNetworkOperatorName();
    
        }
    
    }else{
        //below android API 22
                TelephonyManager tManager = (TelephonyManager) getBaseContext()
                        .getSystemService(Context.TELEPHONY_SERVICE);
    
                String sim1 = tManager.getNetworkOperatorName();
    }
    

    Then to send an SMS you can use the below method

        public static boolean sendSMS(Context context, String smsText) {
        SmsManager smsMan = SmsManager.getDefault();
    
        SharedPreferences prefs = context.getSharedPreferences("uinfo", MODE_PRIVATE);
        String sim = prefs.getString("sim", "No name defined");
        String simName = prefs.getString("simName", "No name defined");
        String toNum = "";
    
        try {
    
            if (Build.VERSION.SDK_INT > 22) {
    
                SubscriptionManager localSubscriptionManager = SubscriptionManager.from(context);
                if (localSubscriptionManager.getActiveSubscriptionInfoCount() > 1) {
    
                    int simID  = Integer.parseInt(sim);
    
                    List localList = localSubscriptionManager.getActiveSubscriptionInfoList();
                    SubscriptionInfo simInfo = (SubscriptionInfo) localList.get(simID);
                    ArrayList parts = smsMan.divideMessage(smsText);
                    SmsManager.getSmsManagerForSubscriptionId(simInfo.getSubscriptionId()).sendMultipartTextMessage(toNum, null, parts, null, null);
                }else{
                    ArrayList parts = smsMan.divideMessage(smsText);
                    smsMan.sendMultipartTextMessage(toNum, null, parts, null, null);
                }
                return true;
            }else{
                ArrayList parts = smsMan.divideMessage(smsText);
                smsMan.sendMultipartTextMessage(toNum, null, parts, null, null);
            }
        } catch (Exception e) {
    
        }
        return false;
    }
    

提交回复
热议问题