Send sms using sim selection option

前端 未结 3 1445
栀梦
栀梦 2021-01-16 19:24

I have tried Android SDK above 5.1 OS. I want to send sms using SIM selection option. I tried this Program. It\'s taking sim1 only. It does not take second sim.

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-16 20:23

    I looked into SubscriptionManager class and found a method to get subsCription info for perticular sim slot

    This worked for me:

             public void sendSMS(final String number,final String text)
    {
        final PendingIntent localPendingIntent1 = PendingIntent.getBroadcast(mContext, 0, new Intent(this.SENT), 0);
        final PendingIntent localPendingIntent2 = PendingIntent.getBroadcast(mContext, 0, new Intent(this.DELIVERED), 0);
    
        if (Build.VERSION.SDK_INT >= 22)
        {
    
            SubscriptionManager subscriptionManager=((Activity)mContext).getSystemService(SubscriptionManager.class);
            SubscriptionInfo subscriptionInfo=subscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(simIndex);
            SmsManager.getSmsManagerForSubscriptionId(subscriptionInfo.getSubscriptionId()).sendTextMessage(number, null, text, localPendingIntent1, localPendingIntent2);
        }
        SmsManager.getSmsManagerForSubscriptionId(subscriptionInfo.getSubscriptionId()).sendTextMessage(number, null, text, localPendingIntent1, localPendingIntent2);
    }
    

    Where simIndex is 0 for sim1 and 1 for sim2

提交回复
热议问题