为什么我的Deliver BroadcastReceiver永远不会被调用?

时间:2017-02-25 16:04:02

标签: android broadcastreceiver sms android-broadcastreceiver smsmanager

我正在通过SmsManager发送消息,我的发送接收器似乎完全按预期工作但我的发送接收器似乎没有被调用。

public void sendSms(BaseFragment fragment, String address, String message) {

    if (isSimAvailable(fragment)) {
        try {
            String SENT = "SMS_SENT";
            String DELIVERED = "SMS_DELIVERED";

            PendingIntent sentPI = PendingIntent.getBroadcast(fragment.getContext(), 0, new Intent(SENT), 0);
            PendingIntent deliverPI = PendingIntent.getBroadcast(fragment.getContext(), 0, new Intent(DELIVERED), 0);

            fragment.getContext().registerReceiver(new SentReceiver(), new IntentFilter(SENT));
            fragment.getContext().registerReceiver(new DeliverReceiver(), new IntentFilter(DELIVERED));

            SmsManager smsMgr = SmsManager.getDefault();
            smsMgr.sendTextMessage(address, null, message, sentPI, deliverPI);
        } catch (Exception e) {
            Toast.makeText(fragment.getContext(), e.getMessage() + "!\n" + "Failed to send SMS", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    } else {
        Toast.makeText(fragment.getContext(), "Cannot send SMS because SIM is not available", Toast.LENGTH_LONG).show();
    }
}

class DeliverReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(context, "Text delivered",
                        Toast.LENGTH_LONG).show();
                break;
            case Activity.RESULT_CANCELED:
                Toast.makeText(context, "sms_not_delivered",
                        Toast.LENGTH_LONG).show();
                break;
        }

    }
}

class SentReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(context, "Sent text", Toast.LENGTH_LONG).show();
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                Toast.makeText(context, "Generic failure", Toast.LENGTH_LONG).show();
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                Toast.makeText(context, "No service", Toast.LENGTH_LONG).show();
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                Toast.makeText(context, "Null PDU", Toast.LENGTH_LONG).show();
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                Toast.makeText(context, "Radio off", Toast.LENGTH_LONG).show();
                break;
        }
    }
}

我添加了以下4个权限。

<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />

同样,我能够发送文本查找,并且调用了我的已发送接收器的onReceive。这只是我的Deliver Receiver上的onReceive没有被调用。

我正在AT&amp; T网络上进行测试,如果这有所不同的话。我确实记得在某个地方读过某些网络没有发回送报告,但如果AT&amp; T的情况如此,我会感到震惊。

0 个答案:

没有答案