K.Liaqat
K.Liaqat

Reputation: 143

Android,Broadcast Receivers activity class wait if its already running

I have an Action class which is called on receiving SMS. Inside of that class,I call an activity and perform some required actions.

public class SMSReceiver extends BroadcastReceiver {

// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();
public static boolean wasScreenOn = true;
Context context;

public void onReceive(Context context, Intent intent) {
    this.context = context;
 // Intent intent = new Intent();
 // starting activity and performing some other action
}

}


// part of AndroidManifest.xml

<receiver android:name="com.**************.********.SMSReceiver">   
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

Problem I am facing is while I am in Activity class and performing some actions, meanwhile if I received another message, main action class is called (SMSReceiver). What I want is to wait the BraoadCast Receiver until I complete my actions in an activity and finishes that activity.

I am new in android and on stackoverflow as well. Sorry If I didn't follow rule (at least I follow 1 rule that I searched throughly for answer before asking this question) or if I fail in asking question properly

Upvotes: 0

Views: 1001

Answers (1)

Ashwin Surana
Ashwin Surana

Reputation: 876

Have a static flag isProcessingMessage = false in SMSReceiver. Whenever Your onReceive is called set it to true and call your Activity.

Now register SMSReceiver for your own created receiver say "sms_processing_finished" and add it to SMSReceiver's intent-filter. <------ Don't forget!!

So when your processing in activity is over, generate a broadcast Receiver having action "sms_processing_finished".

 private void sendSmsProcessingOver() {
  Intent intent = new Intent("sms_processing_finished");
  LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
} 

So now in OnReceive

 public void onReceive(Context context, Intent intent) {
   this.context = context;
   String action = intent.getAction();
   if(action.equals("sms_processing_finished"){
     isProcessingMessage = false;
     /* Now check if any new messages that has arrived is in queue or not.
        if there are messages in      the queue, dequeue them and launch your activity */
   }
   else{
     if(isProcessingMessage){
       /* Queue the Message so that when the processing is over, it will dequeue when 
       it receives "sms_processing_finished" broadcast. */
       return;
     }
     /*do your normal stuff here*/
   }
}

Upvotes: 2

Related Questions