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

问题描述 投票:1回答:1

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

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

当用户输入地理围栏时,会调用Geofence Intent Service,并发送短信。它就像一个魅力,但我需要确保短信已经发送,或者有任何问题(即没有信号),并尝试再次发送。

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

    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);

但仍然没有......

java android service broadcastreceiver smsmanager
1个回答
1
投票

一旦IntentService完成其工作,它将自动停止,并且任何动态注册的接收器也会消失。在你的第一个片段中,当最终发送广播时,接收器很可能无法接收广播,因为sendTextMessage()呼叫将立即返回,并且PendingIntent将被异步触发。

正如您在第二个片段中所做的那样,静态注册Receiver类将允许接收器即使在IntentService死后也能获得广播,因为生成的Receiver实例将不会绑定它。但是,你用于IntentPendingIntent没有正确地定位Receiver类。将其更改为new Intent(this, smsSentReceiver.class)

最后,由于某种原因,logs with certain tags are suppressed"SMS"标签(确切地说)就是其中之一。将该标记更改为未在其中列出的内容;例如,"smsSentReceiver"

© www.soinside.com 2019 - 2024. All rights reserved.