Android短信发送报告无法正常工作

时间:2014-12-04 20:14:48

标签: android android-intent broadcastreceiver android-pendingintent smsmanager

测试硬件: 华为G750双SIM卡

以下代码广播SMS_SENT罚款。虽然接收电话关闭,但它总是播放SMS_Delivered。当接收方收到短信时必须广播。

//SEND SMS
private void SendSMS(int MessageID, String ToMobileNo, String MessageBody, int EncodingID)
{
	// Intent for Sending SMS
	String SendingIntentText = "SMS_SENT" + MessageID;
	Intent SendIntent = new Intent(SendingIntentText);
	SendIntent.putExtra("MessageID", MessageID);
	PendingIntent PendingSendIntent = PendingIntent.getBroadcast(this, 0, SendIntent, 0);

	// SENT SMS Broad Cast Receiver
	registerReceiver(new BroadcastReceiver()
	{
	    @Override
	    public void onReceive(Context arg0, Intent arg1)
	    {
		int MessageID = arg1.getIntExtra("MessageID", 0);
 
		if (MessageID > 0)
		{
		    switch (getResultCode())
		    {
			case Activity.RESULT_OK:
			    DBMethods.SendSMSCounter++;
			    new DBMethods.SendSMSSuccess(MainActivity.this).execute(String.valueOf(MessageID));
			    break;
			// following will call if message failed
			case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
			    Toast.makeText(MainActivity.this, MessageID + " Message Send Failed : RESULT_ERROR_GENERIC_FAILURE ", Toast.LENGTH_LONG).show();
			    break;
			case SmsManager.RESULT_ERROR_NO_SERVICE:
			    Toast.makeText(MainActivity.this, MessageID + " Message Send Failed : RESULT_ERROR_NO_SERVICE ", Toast.LENGTH_LONG).show();
			    break;
			case SmsManager.RESULT_ERROR_NULL_PDU:
			    Toast.makeText(MainActivity.this, MessageID + " Message Send Failed : RESULT_ERROR_NULL_PDU ", Toast.LENGTH_LONG).show();
			    break;
			case SmsManager.RESULT_ERROR_RADIO_OFF:
			    Toast.makeText(MainActivity.this, MessageID + " Message Send Failed : RESULT_ERROR_RADIO_OFF ", Toast.LENGTH_LONG).show();
			    break;
		    }
		}

	    }
	}, new IntentFilter(SendingIntentText));

	// Intent For Delivery Report
	String DeliveryText = "SMS_DELIVERED" + MessageID;
	Intent DeliveryIntent = new Intent(DeliveryText);
	DeliveryIntent.putExtra("MessageID", MessageID);
	PendingIntent PendingDeliveryIntent = PendingIntent.getBroadcast(this, 0, DeliveryIntent, 0);

	// Delivery Report BroadCast Receiver
	registerReceiver(new BroadcastReceiver()
	{
	    @Override
	    public void onReceive(Context arg0, Intent arg1)
	    {
		int MessageID = arg1.getIntExtra("MessageID", 0);
		if (MessageID != 0)
		{
		    switch (getResultCode())
		    {
			case Activity.RESULT_OK:
			    DBMethods.DeliveredSMSCounter++;
			    new DBMethods.SMSDeliverySuccess(MainActivity.this).execute(String.valueOf(MessageID));
			    break;
			case Activity.RESULT_CANCELED:
			    Toast.makeText(MainActivity.this, MessageID + " Message Delivery Failed : RESULT_CANCELED ", Toast.LENGTH_LONG).show();
			    break;
		    }
		}

	    }
	}, new IntentFilter(DeliveryText));

	SmsManager smsManager = SmsManager.getDefault();

	// Encoding English = 0 , Unicode = 2
	if (EncodingID == 0)
	{
	    // 160 chars of single Message.
	    if (MessageBody.length() > 160)
	    {
		ArrayList<String> MessageList = smsManager.divideMessage(MessageBody);

		int MessageParts = MessageList.size();
		ArrayList<PendingIntent> PendingSendIntentList = new ArrayList<PendingIntent>();
		ArrayList<PendingIntent> PendingDeliveryIntentList = new ArrayList<PendingIntent>();

		for (int i = 0; i < MessageParts; i++)
		{
		    PendingSendIntentList.add(PendingSendIntent);
		    PendingDeliveryIntentList.add(PendingDeliveryIntent);
		}
		smsManager.sendMultipartTextMessage(ToMobileNo, null, MessageList, PendingSendIntentList, PendingDeliveryIntentList);
	    }
	    else
	    {
		smsManager.sendTextMessage(ToMobileNo, null, MessageBody, PendingSendIntent, PendingDeliveryIntent);
	    }
	}
	else
	{
	    // converting to bit code UTF-16
	    String UniCodeSMS = null;
	    try
	    {
		byte[] utf16 = MessageBody.getBytes("UTF-16");
		UniCodeSMS = new String(utf16, "UTF-16");
	    }
	    catch (UnsupportedEncodingException ex)
	    {
		Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
	    }

	    // Sending Long SMS
	    if (UniCodeSMS.length() > 70)
	    {
		ArrayList<String> MessageList = smsManager.divideMessage(UniCodeSMS);

		int MessageParts = MessageList.size();
		ArrayList<PendingIntent> PendingSendIntentList = new ArrayList<PendingIntent>();
		ArrayList<PendingIntent> PendingDeliveryIntentList = new ArrayList<PendingIntent>();

		for (int i = 0; i < MessageParts; i++)
		{
		    PendingSendIntentList.add(PendingSendIntent);
		    PendingDeliveryIntentList.add(PendingDeliveryIntent);
		}
		smsManager.sendMultipartTextMessage(ToMobileNo, null, MessageList, PendingSendIntentList, PendingDeliveryIntentList);
	    }
	    else
	    {
		smsManager.sendTextMessage(ToMobileNo, null, UniCodeSMS, PendingSendIntent, PendingDeliveryIntent);
	    }
	}

}

1 个答案:

答案 0 :(得分:1)

&#13;
&#13;
PendingIntent piSent = PendingIntent.getBroadcast(context, 0, sendIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent piDelivered = PendingIntent.getBroadcast(context, 0, deliverIntent, PendingIntent.FLAG_UPDATE_CURRENT);
&#13;
&#13;
&#13;