为什么我没有收到广播?

时间:2014-01-28 14:02:15

标签: android

我有以下代码发送长短信。

正如您在代码中看到的那样,我在部分和部分中截断了长测试 我用SmsManager发送部件

提供PendingIntents的ArrayList(用于短信集和短信发送)。

我为PendingIntents注册了两个BroadcastReceivers。

问题是BroadcastReceivers似乎什么都没收到,所以它们就在那里悬挂,等待显然从未到过的广播......

public void sendViaSms(String nexmoNumber, String message) {
    initSmsReceivers();

    SmsManager sms = SmsManager.getDefault();
            //CREATE THE PARTS OF THE SMS
    ArrayList<String> parts =sms.divideMessage(message);
    int numParts = parts.size();
    ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
    ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
    //POPULATE THE ARRAY OF PENDINGINTENTS
            for (int i = 0; i < numParts; i++) {
    sentIntents.add(PendingIntent.getBroadcast(MessageCompose.this, 0,  new Intent(SENT), 0));
    deliveryIntents.add(PendingIntent.getBroadcast(MessageCompose.this, 0, new Intent(DELIVERED), 0));
    }
            //SEND THE SMS IN PARTS WITH THE ARRAY OF PENDINGINTENTS
    sms.sendMultipartTextMessage(nexmoNumber,null, parts, sentIntents, deliveryIntents);
}   

    //CREATE AND REGISTER THE BROADCASTRECEIVERS 
private void initSmsReceivers() {
    if (receiversInitialized) {
        return;
    }

    receiversInitialized = true;

    sendBroadcastReceiver = new BroadcastReceiver() {

        public void onReceive(Context contex, Intent intent) {
            switch (getResultCode()) {
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
            case SmsManager.RESULT_ERROR_NO_SERVICE:
            case SmsManager.RESULT_ERROR_NULL_PDU:
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                progressDialog.cancel();
                showMessage(R.string.error, R.string.email_send_failed);
                break;
            }
        }
    };

    deliveryBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context contex, Intent intent) {
            if (!progressDialog.isShowing()) {
                return;
            }

            switch (getResultCode()) {
            case Activity.RESULT_OK:
                showSuccessDialog();
                break;
            default:
                showMessage(R.string.error, R.string.email_send_failed);
                break;
            }

            progressDialog.cancel();


        }
    };

    MessageCompose.this.registerReceiver(deliveryBroadcastReceiver,
            new IntentFilter(DELIVERED));
    MessageCompose.this.registerReceiver(sendBroadcastReceiver,
            new IntentFilter(SENT));

}

1 个答案:

答案 0 :(得分:1)

您是否在清单中声明了许可? (使用适当的)

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