繁体   English   中英

通过SmsManager在双SIM卡Android手机中发送短信

[英]Send sms in dual sim android phone by SmsManager

我想通过SmsManager发送短信,但首先在这种情况下,我无法平衡短信失败。 所以如何仅通过我的应用程序通过第二个SIM发送短信。 其他应用程序,例如whatsApp,如果在第一个SIM卡中没有可用余额,则会在注册时间从第二个SIM卡发送味精,所以我认为这是可能的。 请帮助我

String mobileNo = phoneNumber1.getText().toString();

  try {
    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(mobileNo, null, "BAL", null, null);
    Toast.makeText(getApplicationContext(), "SMS Sent!",
                Toast.LENGTH_LONG).show();
  } catch (Exception e) {
    Toast.makeText(getApplicationContext(),
        "SMS faild, please try again later!",
        Toast.LENGTH_LONG).show();
    e.printStackTrace();
  }

我查看了SubscriptionManager类,发现了一个getActiveSubscriptionInfoForSimSlotIndex(simIndex)方法,可以使用此subscriptionInfo获取垂直sim插槽的subscriptionInfo,我们可以获取垂直simSlot的subscriptionId。 使用subscriptionID,我们可以获取所需SimSlot的SMSManager

这为我工作:

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);
}

其中simIndex对于sim1为0,对于sim2为1

在分析了Mlais MX28手机(带有定制的ROM)的日志后,我发现了以下技巧来切换SIM卡以发送短信。 不幸的是,它似乎在全球范围内为所有应用切换了SIM卡(我还无法找到一种方法来“保存和恢复”当前设置)。 另外,您可能需要麻烦找出分配给每个SIM卡的ID( sim_id )(可能是通过检查手机的日志输出;我也找不到编程方式来执行此操作)。

int simId = <place desired SIM ID here>;
Intent simIntent = new Intent("android.intent.action.SMS_DEFAULT_SIM");
simIntent.putExtra("simid", simId);
sendBroadcast(simIntent);

我不确定这是否对您有用(尽管代码似乎与制造商无关); 但我想仍然值得一试。

更新:

奇怪的是,经过一些尝试,以前的方法不再起作用。 但是,经过更多日志分析后,我遇到了另一种使用直接设置操作的方法(不幸的是,这需要android.permission.WRITE_SETTINGS权限)。

ContentValues val = new ContentValues();
val.put("value", "here goes the preferred SIM ID");
getContentResolver().update(Uri.parse("content://settings/system"), val, "name='sms_sim_setting'", null);

我相信这是适应于其他多SIM-关注操作为好,因为我的设备上的设置数据库包含像设置的条目gprs_connection_sim_settingvoice_call_sim_settingvideo_call_sim_setting等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM