儀式現在我試圖通過編程方式使用以下代碼發送短信,但我不明白SMS SENT Receiver的行爲。短信通知的行爲
1)例如,如果我發送一個SMS然後
Activity.RESULT_OK
內部registerReceiver
正在被呼叫的3倍。如果我發送3短信使用 爲loopby調用sendSMS
然後Activity.RESULT_OK
正在 調用9次。現在我真的不知道一個短信發送爲什麼這個 registerReceiver被稱爲這麼多次?2)此外,當我運行這段代碼在模擬器我通過模擬器 端口發送短信到其他仿真這是很自然的,但是當我嘗試 發送短信到一個真實的號碼,然後我沒有得到短信發送失敗 通知,因爲它通知
Activity.RESULT_OK
僅
代碼發送SMS
private void sendSMS(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
Log.d("SMS Service", "SMS SEND CALLED");
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
Log.d("SMS Service", "RECEIVE CALLED");
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(SMSService.this, "SMS sent",
Toast.LENGTH_SHORT).show();
System.out.println("SMSService " + "SMS SENT");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(SMSService.this, "Generic failure",
Toast.LENGTH_SHORT).show();
System.out.println("SMSService " + "GENERIC FAILURE");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(SMSService.this, "No service",
Toast.LENGTH_SHORT).show();
System.out.println("SMSService " + "NO SERVICE");
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(SMSService.this, "Null PDU",
Toast.LENGTH_SHORT).show();
System.out.println("SMSService " + "Null PDU");
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(SMSService.this, "Radio off",
Toast.LENGTH_SHORT).show();
System.out.println("SMSService " + "Radio Off");
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
System.out.println("SMSService " + "SMS Delivered");
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
System.out.println("SMSService " + "SMS not delivered");
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
以及我的短信文本是「John Right,這是我的簡單信息!」 ,所以這段文字沒有那麼長吧?那麼仍然需要使用divideMessage? – Hunt 2012-03-13 08:26:26
還有一個需要(由於HTC的bug - 這是令人討厭的,然後它擊中我的應用程序),但它不應該導致未決意圖的三重響應。 – zeetoobiker 2012-03-13 09:21:10
只是更新了另一種可能性和一些示例代碼,至少適合我! – zeetoobiker 2012-03-13 09:36:54