0

I have used broadcastreceiver to handle a phone number verification part. It works but in a problematic way.

Issue: When there is no message , onReceive() just execute once and return from if (extras == null){... part. this is fine. But if message sent to inbox , onReceive() called null i.e if (extras == null){.. part first(in 1 seconds) and then executes the original part again(after 3/4 seconds). Some of my codes:

In onCreate():

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

In Button Click:

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

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

Am I doing any wrong here ?

Ranjit
  • 5,130
  • 3
  • 30
  • 66
  • I'm not fully understanding your description, but why are you broadcasting (or attempting to broadcast) the `android.provider.Telephony.SMS_RECEIVED` action? This is handled by the system. – Mike M. Aug 10 '14 at 07:27
  • @MikeM. i am working on mobile number verification part. i.e at the time of user registration i have to verify that the user's mobile number is perfect or not by sending a message to its own mobile and handle if the message reached. All I can do by the help of `android.provider.Telephony.SMS_RECEIVED` broadcast. – Ranjit Aug 10 '14 at 07:31
  • I still don't fully understand, but your app should _not_ be broadcasting that action. – Mike M. Aug 10 '14 at 07:34
  • @MikeM. I am using your answer only. from http://stackoverflow.com/questions/20133774/use-sms-to-verify-devices-phone-number .. – Ranjit Aug 10 '14 at 07:36
  • Yeah, I figured you were, but nowhere in that post do I broadcast that action. All you need to do is register the Receiver for that action, and send the message. – Mike M. Aug 10 '14 at 07:38
  • But when the verification mobile number is incorrect( i.e new no message in inbox), it just executes once( returning from extra == null)..but when there is a new message(i.e number is perfect), it executes twice..first returning from `extra==null` part and again executes the same onreceive with perfect one. – Ranjit Aug 10 '14 at 07:39
  • But you told to register in activity:in this sentence... `If you don't need to monitor every SMS received, I would recommend registering it in your Activity for just as long as you need it.` – Ranjit Aug 10 '14 at 07:40
  • 1
    Right, register, not broadcast. If the number isn't correct, the device isn't going to receive an incoming SMS, so you'll need to set a time-out for receiving the test message. – Mike M. Aug 10 '14 at 07:43
  • @MikeM. Its good if you show some path in an answer which will help me to understand the problem and resolve. It may helpful for others also. – Ranjit Aug 10 '14 at 07:45
  • I'll update that answer when I'm able to later. I'm on a mobile at the moment. For now, I'd just recommend removing the broadcast, and leaving any dependent functionality disabled until a confirmation SMS is verified. – Mike M. Aug 10 '14 at 07:51
  • @MikeM. Its ok Mike. I am trying to resolve. thanks for your time :) – Ranjit Aug 10 '14 at 07:54
  • In fact, that would be how I'd recommend handling it. There's no need for a timeout. Just don't enable the necessary functionality _until_ the number has been verified. – Mike M. Aug 10 '14 at 08:21
  • Thanks Mike.I got solution from your comments.I think first of all i have to delete 3 main sentences (i.e `Intent i = new Intent(); i.setAction("android.provider.Telephony.SMS_RECEIVED"); sendBroadcast(i);`). Am I right.. – Ranjit Aug 10 '14 at 08:26
  • Yep. That should do it. When you get the incoming SMS and verify the number, then enable whatever you need to. – Mike M. Aug 10 '14 at 08:28
  • @MikeM. Can you help me to do this in dual SIM devices. I am facing problem in here. – Ranjit Dec 12 '14 at 05:30
  • I have asked the question here http://stackoverflow.com/questions/27437699/mobile-number-verification-for-dual-sim-devices-in-android – Ranjit Dec 12 '14 at 06:17
  • I've not done anything with dual SIMs before, and the versions of Android I work with don't directly support them. I think Lollipop does, but I've not done anything with that, either. – Mike M. Dec 12 '14 at 11:34
  • 1
    No Problem Mike..By the way Thanks for your support :) – Ranjit Dec 12 '14 at 11:42

0 Answers0