通知您发送的消息是否在Android中成功发送 [英] Notify if a message you sent was sent successfully or not in Android

查看:68
本文介绍了通知您发送的消息是否在Android中成功发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有一些代码可以发送消息:

I have some code here to send a message:

SmsManager sms = new SmsManager.getDefault();
sms.sendTextMessage("911", null, "HALP!", PendingIntent, null);

Developer.android关于 PendingIntent 的说法是这样的:

Developer.android says this about PendingIntent:

如果不是 NULL ,则此 PendingIntent 在成功发送或失败消息时广播.成功的结果代码将为 Activity.RESULT_OK 或以下错误之一:

if not NULL this PendingIntent is broadcast when the message is successfully sent, or failed. The result code will be Activity.RESULT_OK for success, or one of these errors:

RESULT_ERROR_GENERIC_FAILURE

RESULT_ERROR_RADIO_OFF

RESULT_ERROR_NULL_PDU

对于 RESULT_ERROR_GENERIC_FAILURE sentIntent 可能包含额外的"errorCode",包含特定于无线电技术的值,通常仅用于故障排除.

For RESULT_ERROR_GENERIC_FAILURE the sentIntent may include the extra "errorCode" containing a radio technology specific value, generally only useful for troubleshooting.

基于每个应用程序的SMS控件检查 sentIntent .如果 sentIntent NULL ,则将针对所有未知应用程序检查呼叫者,这将导致在检查期间发送较少数量的SMS.

The per-application based SMS control checks sentIntent. If sentIntent is NULL the caller will be checked against all unknown applications, which cause smaller number of SMS to be sent in checking period.

我的问题是:我如何制作一个 PendingIntent 放入 sendTextMessage 中,该内容仅显示一个 Toast ,说明已发送邮件还是不?

My Question is: How can I make a PendingIntent to feed into the sendTextMessage that simply shows a Toast saying either the message was sent or not?

谢谢.

推荐答案

这可以通过向 BroadcastReceiver 注册您的 PendingIntent 来实现,只需在消息发送时通知您以及何时交付给预期的接收者.只需按照以下步骤_

This can be achieve simply by registering your PendingIntent with BroadcastReceiver that notify you when your Message is SEND and when it DELIVERED to intended recipient. Just follow below steps_

  1. 构建两个 PendingIntent ,一个用于发送,另一个用于传递通知.
  2. 构建两个 BroadcastReceiver .一个用于发送,另一个用于传递.
  3. 最后,通过 registerReceiver 向相应的 BroadcastReceiver 注册这些PendingIntent,以便通知他们的 BroadcastReceiver 并执行Needfull.
  1. Build two PendingIntent one for send and other for deliver notification.
  2. Build two BroadcastReceiver. One for send and another for deliver.
  3. Finally, register these PendingIntent with respective BroadcastReceiver through registerReceiver, So that their BroadcastReceiver get notified and do the needfull.

我已经建立了一个简单的类来完成上面已经解释过的工作.您可以通过本课程来完全了解它_

I have build one simple class that do the work that i have explained above. You may go through this class to have complete understanding of it_

public class SendDeliverSMSActivity extends Activity {
Button btnSendSMS;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
    btnSendSMS.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Your method that send sms
            sendSMS("5556", "Hi You got a message!");
        }
    });

}

/**
 * ---sends an SMS message to another device---
 * 
 * @param phoneNumber
 * @param message
 */
private void sendSMS(String phoneNumber, String message) {
    // Intent Filter Tags for SMS SEND and DELIVER
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";
// STEP-1___
    // SEND PendingIntent
    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
            SENT), 0);

    // DELIVER PendingIntent
    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
            new Intent(DELIVERED), 0);
// STEP-2___
    // SEND BroadcastReceiver
    BroadcastReceiver sendSMS = new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(getBaseContext(), "SMS sent",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                Toast.makeText(getBaseContext(), "Generic failure",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                Toast.makeText(getBaseContext(), "No service",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                Toast.makeText(getBaseContext(), "Null PDU",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                Toast.makeText(getBaseContext(), "Radio off",
                        Toast.LENGTH_SHORT).show();
                break;
            }
        }
    };

    // DELIVERY BroadcastReceiver
    BroadcastReceiver deliverSMS = 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();
                break;
            case Activity.RESULT_CANCELED:
                Toast.makeText(getBaseContext(), "SMS not delivered",
                        Toast.LENGTH_SHORT).show();
                break;
            }
        }
    };
// STEP-3___
    // ---Notify when the SMS has been sent---
    registerReceiver(sendSMS, new IntentFilter(SENT));

    // ---Notify when the SMS has been delivered---
    registerReceiver(deliverSMS, new IntentFilter(DELIVERED));

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
 }
}

我希望这会对您有所帮助!:)

I hope this will help you! :)

这篇关于通知您发送的消息是否在Android中成功发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆