11

I have an Activity that calls a Broadcast Receiver. The Broadcast Receiver waits and listens to GPS. When the listener gets the new point I want to send that new point to Activity. How can I send data from Broadcast Receiver to Activity?

I need a listener in my Activity waiting for response from Broadcast Receiver. How can I do that?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Bob
  • 22,810
  • 38
  • 143
  • 225

4 Answers4

22

You can call the receiver from your activity. If you don't want to add the logic of the receiver in you activity you can use an abstract receiver.

You abstract receiver:

public abstract class SmsReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
           //Add you receiver logic here
           ...
           ...
           onNewPosition();
        }

    protected abstract void onNewPosition();

}

In your activity:

public class MyActivity extends Activity {

    private smsReceiver smsReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_one_position);
        smsReceiver = new smsReceiver() {

            // this code is call asyncrously from the receiver
            @Override
            protected void onNewPosition() {
            //Add your activty logic here
            }

        };
        IntentFilter intentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
        intentFilter.setPriority(999);
        this.registerReceiver(smsReceiver, intentFilter);

    }

     @Override
        protected void onPause() {
            super.onPause();
            this.unregisterReceiver(this.smsReceiver);
        }

}

I hope it will help you...

user1853984
  • 359
  • 2
  • 7
4

I defined a listener for my receiver and use it in activity and it is running perfect now. Is it possible to happen any problem later?

public interface OnNewLocationListener {
public abstract void onNewLocationReceived(Location location);

}

in My receiver class wich is named as ReceiverPositioningAlarm:

// listener ----------------------------------------------------

static ArrayList<OnNewLocationListener> arrOnNewLocationListener =
        new ArrayList<OnNewLocationListener>();

// Allows the user to set an Listener and react to the event
public static void setOnNewLocationListener(
        OnNewLocationListener listener) {
    arrOnNewLocationListener.add(listener);
}

public static void clearOnNewLocationListener(
        OnNewLocationListener listener) {
    arrOnNewLocationListener.remove(listener);
}

// This function is called after the new point received
private static void OnNewLocationReceived(Location location) {
    // Check if the Listener was set, otherwise we'll get an Exception when
    // we try to call it
    if (arrOnNewLocationListener != null) {
        // Only trigger the event, when we have any listener
        for (int i = arrOnNewLocationListener.size() - 1; i >= 0; i--) {
            arrOnNewLocationListener.get(i).onNewLocationReceived(
                    location);
        }
    }
}

and in one of my activity's methods:

OnNewLocationListener onNewLocationListener = new OnNewLocationListener() {
        @Override
        public void onNewLocationReceived(Location location) {
            // do something

            // then stop listening
            ReceiverPositioningAlarm.clearOnNewLocationListener(this);
        }
    };

    // start listening for new location
    ReceiverPositioningAlarm.setOnNewLocationListener(
            onNewLocationListener);
Bob
  • 22,810
  • 38
  • 143
  • 225
3

you have couple of ways you can do it and several considerations.

  1. you can poll, meaning check every now an again using either Handler or Timer to see if info has arrived.

  2. you can register the broadcast receiver as an inner class of your activity and then you can call methods in your activty.

  3. you can have the Broadcast send Intent to your class with the info, but if your activity is not in foreground you might bring it there , and that's not 100% what you want...

Regarding some consideration, BroadCastReciver is mainly used as a listener, not notider so inner class, is best practice, in my opinion, for use with Activities, for Services you can use it as a standalone class and register it in the Manifest.xml... Now you got to remember that when broadcast is being broadcast your Activity might be inactive due to orientation change or event that pauses your app so you might miss the event. i don't listen to system events but to my own events so i use sticky broadcast to prevent that issue.

codeScriber
  • 4,582
  • 7
  • 38
  • 62
  • thanks. in the third solution, how can I get that intent. could you please explain more? – Bob Jul 13 '11 at 04:48
  • sure. You send the intent using context.startActivity() you get the intent in the activity in one of two ways, either using getIntent method in onCreate or onResume or in onNewIntent override method. the question is whether or not your activity is restarted, you can read more about it in the Activity java doc and Intent java doc, specifically the one that deals with Intent flags (SINGLE_TOP) – codeScriber Jul 13 '11 at 05:42
  • you have decided to use the Listener pattern, it could work ok, did for me However DON'T FORGET' if one of your listener is an Activity you leak out the context if you get Orientation or other configuration change event that causes the application to die and get recreated, in that case you got to change your onResume and onPause code (or on Create and onDestroy) that will remove the listener and put it back on once the activity returns. – codeScriber Jul 17 '11 at 08:23
  • oh, and after the activity returns and you set the listener again, u got to check if you didn't miss any event that happen during that time... – codeScriber Jul 17 '11 at 08:23
0

Just need to implement the broadcast receiver in the activity. register the receiver with activity's context.

Plamen Nikolov
  • 4,177
  • 3
  • 23
  • 24