0
測試硬件: 華爲G750雙卡Android的短信送達報告正確
下面的代碼廣播SMS_SENT fine.While它始終廣播SMS_Delivered即使接收手機關機。當接收者收到短信時必須廣播。
//SEND SMS
private void SendSMS(int MessageID, String ToMobileNo, String MessageBody, int EncodingID)
{
\t // Intent for Sending SMS
\t String SendingIntentText = "SMS_SENT" + MessageID;
\t Intent SendIntent = new Intent(SendingIntentText);
\t SendIntent.putExtra("MessageID", MessageID);
\t PendingIntent PendingSendIntent = PendingIntent.getBroadcast(this, 0, SendIntent, 0);
\t // SENT SMS Broad Cast Receiver
\t registerReceiver(new BroadcastReceiver()
\t {
\t @Override
\t public void onReceive(Context arg0, Intent arg1)
\t {
\t \t int MessageID = arg1.getIntExtra("MessageID", 0);
\t \t if (MessageID > 0)
\t \t {
\t \t switch (getResultCode())
\t \t {
\t \t \t case Activity.RESULT_OK:
\t \t \t DBMethods.SendSMSCounter++;
\t \t \t new DBMethods.SendSMSSuccess(MainActivity.this).execute(String.valueOf(MessageID));
\t \t \t break;
\t \t \t // following will call if message failed
\t \t \t case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
\t \t \t Toast.makeText(MainActivity.this, MessageID + " Message Send Failed : RESULT_ERROR_GENERIC_FAILURE ", Toast.LENGTH_LONG).show();
\t \t \t break;
\t \t \t case SmsManager.RESULT_ERROR_NO_SERVICE:
\t \t \t Toast.makeText(MainActivity.this, MessageID + " Message Send Failed : RESULT_ERROR_NO_SERVICE ", Toast.LENGTH_LONG).show();
\t \t \t break;
\t \t \t case SmsManager.RESULT_ERROR_NULL_PDU:
\t \t \t Toast.makeText(MainActivity.this, MessageID + " Message Send Failed : RESULT_ERROR_NULL_PDU ", Toast.LENGTH_LONG).show();
\t \t \t break;
\t \t \t case SmsManager.RESULT_ERROR_RADIO_OFF:
\t \t \t Toast.makeText(MainActivity.this, MessageID + " Message Send Failed : RESULT_ERROR_RADIO_OFF ", Toast.LENGTH_LONG).show();
\t \t \t break;
\t \t }
\t \t }
\t }
\t }, new IntentFilter(SendingIntentText));
\t // Intent For Delivery Report
\t String DeliveryText = "SMS_DELIVERED" + MessageID;
\t Intent DeliveryIntent = new Intent(DeliveryText);
\t DeliveryIntent.putExtra("MessageID", MessageID);
\t PendingIntent PendingDeliveryIntent = PendingIntent.getBroadcast(this, 0, DeliveryIntent, 0);
\t // Delivery Report BroadCast Receiver
\t registerReceiver(new BroadcastReceiver()
\t {
\t @Override
\t public void onReceive(Context arg0, Intent arg1)
\t {
\t \t int MessageID = arg1.getIntExtra("MessageID", 0);
\t \t if (MessageID != 0)
\t \t {
\t \t switch (getResultCode())
\t \t {
\t \t \t case Activity.RESULT_OK:
\t \t \t DBMethods.DeliveredSMSCounter++;
\t \t \t new DBMethods.SMSDeliverySuccess(MainActivity.this).execute(String.valueOf(MessageID));
\t \t \t break;
\t \t \t case Activity.RESULT_CANCELED:
\t \t \t Toast.makeText(MainActivity.this, MessageID + " Message Delivery Failed : RESULT_CANCELED ", Toast.LENGTH_LONG).show();
\t \t \t break;
\t \t }
\t \t }
\t }
\t }, new IntentFilter(DeliveryText));
\t SmsManager smsManager = SmsManager.getDefault();
\t // Encoding English = 0 , Unicode = 2
\t if (EncodingID == 0)
\t {
\t // 160 chars of single Message.
\t if (MessageBody.length() > 160)
\t {
\t \t ArrayList<String> MessageList = smsManager.divideMessage(MessageBody);
\t \t int MessageParts = MessageList.size();
\t \t ArrayList<PendingIntent> PendingSendIntentList = new ArrayList<PendingIntent>();
\t \t ArrayList<PendingIntent> PendingDeliveryIntentList = new ArrayList<PendingIntent>();
\t \t for (int i = 0; i < MessageParts; i++)
\t \t {
\t \t PendingSendIntentList.add(PendingSendIntent);
\t \t PendingDeliveryIntentList.add(PendingDeliveryIntent);
\t \t }
\t \t smsManager.sendMultipartTextMessage(ToMobileNo, null, MessageList, PendingSendIntentList, PendingDeliveryIntentList);
\t }
\t else
\t {
\t \t smsManager.sendTextMessage(ToMobileNo, null, MessageBody, PendingSendIntent, PendingDeliveryIntent);
\t }
\t }
\t else
\t {
\t // converting to bit code UTF-16
\t String UniCodeSMS = null;
\t try
\t {
\t \t byte[] utf16 = MessageBody.getBytes("UTF-16");
\t \t UniCodeSMS = new String(utf16, "UTF-16");
\t }
\t catch (UnsupportedEncodingException ex)
\t {
\t \t Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
\t }
\t // Sending Long SMS
\t if (UniCodeSMS.length() > 70)
\t {
\t \t ArrayList<String> MessageList = smsManager.divideMessage(UniCodeSMS);
\t \t int MessageParts = MessageList.size();
\t \t ArrayList<PendingIntent> PendingSendIntentList = new ArrayList<PendingIntent>();
\t \t ArrayList<PendingIntent> PendingDeliveryIntentList = new ArrayList<PendingIntent>();
\t \t for (int i = 0; i < MessageParts; i++)
\t \t {
\t \t PendingSendIntentList.add(PendingSendIntent);
\t \t PendingDeliveryIntentList.add(PendingDeliveryIntent);
\t \t }
\t \t smsManager.sendMultipartTextMessage(ToMobileNo, null, MessageList, PendingSendIntentList, PendingDeliveryIntentList);
\t }
\t else
\t {
\t \t smsManager.sendTextMessage(ToMobileNo, null, UniCodeSMS, PendingSendIntent, PendingDeliveryIntent);
\t }
\t }
}
這是什麼問題?你得到了什麼錯誤? – 2014-12-04 20:19:51
沒有錯誤。當收到發件人的短信時應收到遞送報告。而我的代碼總是廣播傳遞報告,這是問題。 – MSajjad 2014-12-04 20:23:23
嗨@MSajjad,你有沒有找到解決方案?謝謝。 – teapeng 2015-03-17 08:34:29