簡體   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