2014-04-15 133 views
0

我想通過SmsManager發送短信的工作,但在第一SIM我在這種情況下,沒有平衡短信失敗。所以如何僅通過第二個SIM卡從我的應用程序發送短信。 其他應用程序,如whatsApp在註冊時從我的第二個SIM卡發送味精,如果不是在第一個sim.so平衡可用。我認爲它可能。請幫我發送短信雙卡Android手機通過SmsManager

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(); 
    } 
+0

看到http://stackoverflow.com/questions/5255147 0 SIM1和1/dual-sim-card-android –

+0

@shayanpourvatan謝謝你,但它不是我的答案。因爲whatsApp在註冊時發送msg在我的第二個sim,所以我認爲它可能。請幫我 – Ranu

+0

我不認爲WhatsApp發送短信,它只收到一個。 – hitmaneidos

回答

0

分析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_settingvideo_call_sim_setting設置,voice_call_sim_setting,條目等

0

我看着SubscriptionManager類,並找到一個方法getActiveSubscriptionInfoForSimSlotIndex(simIndex)來獲取subscriptionInfo for perticular sim slot使用這個subscriptionInfo我們可以得到perticular simSlot的subscriptionId。使用subscriptionID我們可以得到SMSManager爲所需simSlot

這爲我工作:

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是SIM2

相關問題