从 Internet 服务发送短信 - 无法接收广播以检查是否已发送短信

Sending SMS from Intent Service - Unable to recieve broadcast to check if sms was sent

我的应用程序是关于地理围栏的。当触发地理围栏时,我需要发送短信。

它几乎完美运行,但我无法接收广播来检查是否已发送短信。我会解释我自己:

当用户进入地理围栏时,会调用地理围栏意图服务,并发送短信。它就像一个魅力,但我需要确定短信是否已发送,或者是否有任何问题(即没有信号)并尝试再次发送。

所以,首先我尝试用这种方式发送短信:

    private void sendsms() {

        String SMS_SENT = "SMS_SENT";
        final PendingIntent sentPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);

// check if SMS has been sent
        smsSentReceiver =new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Log.e("SMS", "SMS sent successfully");
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Log.e("SMS","Generic failure cause");
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Log.e("SMS", "Service is currently unavailable");
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Log.e("SMS", "No pdu provided");
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Log.e("SMS", "Radio was explicitly turned off");
                        break;
                }
            }
        };


        registerReceiver(smsSentReceiver, new Intent());

        // Get the default instance of SmsManager
        // Send a text based SMS
        SmsManager smsMgr = SmsManager.getDefault();
        smsMgr.sendTextMessage(towhom, null, customtxt, sentPendingIntent, null);

这有效,但发送短信后没有任何反应,根本没有日志。

然后我尝试了另一种方式,创建接收器并放入清单:

private smsSentReceiver smsSentReceiver;

private void sendsms() {

        final PendingIntent sentPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);

        smsSentReceiver = new smsSentReceiver();
        registerReceiver(smsSentReceiver, new IntentFilter());

        // Get the default instance of SmsManager
        // Send a text based SMS
        SmsManager smsMgr = SmsManager.getDefault();
        smsMgr.sendTextMessage(towhom, null, customtxt, sentPendingIntent, null);

这是smsSentReceiver.java

public class smsSentReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        switch (getResultCode()) {
            case Activity.RESULT_OK:
                Log.e("SMS", "SMS sent successfully");
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                Log.e("SMS","Generic failure cause");
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                Log.e("SMS", "Service is currently unavailable");
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                Log.e("SMS", "No pdu provided");
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                Log.e("SMS", "Radio was explicitly turned off");
                break;
        }
    }
}

和清单:

<receiver
            android:name=".smsSentReceiver"
            android:enabled="true"
            >

        </receiver>

根本没有关于短信状态的日志...

关于为什么我没有收到短信状态有什么想法吗?

PS: 只在模拟器上测试过

编辑:

根据@Mike M 的说法,我最后尝试的是这个。评论:

private void sendsms() {

        final PendingIntent sentPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, smsSentReceiver.class), PendingIntent.FLAG_CANCEL_CURRENT);

 SmsManager smsMgr = SmsManager.getDefault();
        smsMgr.sendTextMessage(towhom, null, customtxt, sentPendingIntent, null);

还是什么都没有...

IntentService 将在完成工作后立即自行停止,并且在其上动态注册的所有接收器也会消失。在您的第一个片段中,接收者在最终发送广播时可能无法接收广播,因为 sendTextMessage() 调用将立即 return,并且 PendingIntent 将被异步触发。

静态注册接收器 class,正如您在第二个代码段中所做的那样,即使在 IntentService 已死亡后,接收器仍能接收广播,因为生成的接收器实例将不要被它束缚。但是,您用于 PendingIntentIntent 没有正确定位接收器 class。将其更改为 new Intent(this, smsSentReceiver.class).

最后,,出于某种原因,"SMS" 标签(确切地说)就是其中之一。将该标签更改为此处未列出的内容;例如,"smsSentReceiver".