Telephony.SMS_RECEIVED:onReceive()调用两次

时间:2014-08-10 07:18:09

标签: android broadcastreceiver messaging

我使用broadcastreceiver来处理电话号码验证部分。它有效,但有问题。

问题: 当没有消息时,onReceive()只执行一次并从if (extras == null){...部分返回。这可以。但是如果发送到收件箱的消息,onReceive()首先调用空,if (extras == null){..部分(在1秒内),然后再次执行原始部分(3/4秒后)。 我的一些代码:

在onCreate()中:

 receiver = new SMSReceiver();
 filter = new IntentFilter();
 filter.addAction("android.provider.Telephony.SMS_RECEIVED");

按钮点击:

registerReceiver(receiver, filter);
mProgressDialog = MyProgressDialog.show(RegisterActivity.this, null, null);

SmsManager sm = SmsManager.getDefault();
sm.sendTextMessage(mobileNumber, null, "Verified", null, null);
Intent i = new Intent();
i.setAction("android.provider.Telephony.SMS_RECEIVED");
sendBroadcast(i);

的onReceive():

private  class SMSReceiver extends BroadcastReceiver{

@Override
  public void onReceive(Context context, Intent intent) {

  if(intent.getAction() != null && intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){

  Bundle extras = intent.getExtras();

  if (extras == null){
    _submit.setText("Register");
    _mobile_number.setError("Invalid Number");
    mProgressDialog.dismiss();
    Toast.makeText(RegisterActivity.this, "Invalid Mobile Number", ast.LENGTH_LONG).show();
    return;
 }

try{

    Object[] pdus = (Object[]) extras.get("pdus");
    SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdus[0]);
    String origNumber = msg.getOriginatingAddress();
    String msgBody = msg.getMessageBody();

   ..............   

我在这里做错了吗?

0 个答案:

没有答案