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