在BroadcastReceiver中,getResultCode()始终为SMS发送和传递的操作返回-1

时间:2012-09-01 06:23:05

标签: android sms broadcastreceiver

我想大多数人都是,我从http://mobiforge.com/developing/story/sms-messaging-android获取了我的短信处理代码。我正在尝试使用此代码(针对已发送的消息)捕获已发送和已发送消息的广播:

        appContext.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                Log.i("Debug",
                        "My embedded receiver: "
                                + String.valueOf(getResultCode()));
            }
        }, new IntentFilter("iam.applications.SmsReceiver.SMS_SENT"));

这总是会导致getResultCode()返回值-1,即使成功发送了SMS也是如此。我不知道另一种方式来判断我的短信是否已被发送。

我还将BroadcastReceiver子类化,看看是否有任何区别,但事实并非如此。始终返回-1:

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    Log.i("Debug",
            "Result code in onReceive: " + String.valueOf(getResultCode()));
}

我对PendingIntent的创建看起来像这样(来自for循环的片段用于处理长SMS消息的部分):

        Intent sentIntent = new Intent(
                "iam.applications.SmsReceiver.SMS_SENT");
        sentIntent.putExtra(SmsHandler.PHONE_NUMBER, phoneNumber);
        sentIntent.putExtra(SmsHandler.MESSAGE, message);
        sentPIArray.add(PendingIntent.getBroadcast(appContext,
                (int) Calendar.getInstance().getTimeInMillis(), sentIntent,
                PendingIntent.FLAG_UPDATE_CURRENT));

2 个答案:

答案 0 :(得分:3)

我是一个大白痴:

// Field descriptor #14 I
public static final int RESULT_OK = -1;

答案 1 :(得分:0)

要获取发送和发送报告,我这样做了:

String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";

BroadcastReceiver sendingBroadcastReceiver;
BroadcastReceiver deliveryBroadcastReceiver;

PendingIntent sentPI;
PendingIntent deliveredPI;

public void sendSms(String phoneNumber, String message) {
    sentPI      = PendingIntent.getBroadcast(this, 0, new Intent(SENT),      PendingIntent.FLAG_CANCEL_CURRENT);
    deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), PendingIntent.FLAG_CANCEL_CURRENT);

    sendingBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    break;
            }
            BaseActivity.this.unregisterReceiver(this);
        }
    };
}

this.registerReceiver(sendingBroadcastReceiver, new IntentFilter(SENT));

    deliveryBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (getResultCode()) {
            case Activity.RESULT_OK:
                break;
            case Activity.RESULT_CANCELED:
                break;                        
            }
            BaseActivity.this.unregisterReceiver(this);
        }
    };

this.registerReceiver(deliveryBroadcastReceiver, new IntentFilter(DELIVERED));   

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