Home » Android » android – How can I send result data from Broadcast Receiver to Activity

android – How can I send result data from Broadcast Receiver to Activity

Posted by: admin May 14, 2020 Leave a comment

Questions:

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?

How to&Answers:

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…

Answer:

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

Answer:

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.

Answer:

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