Skip to content Skip to sidebar Skip to footer

How Can I Send Result Data From Broadcast Receiver To Activity

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.

Solution 1:

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:

publicabstractclassSmsReceiverextendsBroadcastReceiver {

    @OverridepublicvoidonReceive(Context context, Intent intent) {
           //Add you receiver logic here
           ...
           ...
           onNewPosition();
        }

    protectedabstractvoidonNewPosition();

}

In your activity:

publicclassMyActivityextendsActivity {

    private smsReceiver smsReceiver;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_one_position);
        smsReceiver = newsmsReceiver() {

            // this code is call asyncrously from the receiver@OverrideprotectedvoidonNewPosition() {
            //Add your activty logic here
            }

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

    }

     @OverrideprotectedvoidonPause() {
            super.onPause();
            this.unregisterReceiver(this.smsReceiver);
        }

}

I hope it will help you...

Solution 2:

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?

publicinterfaceOnNewLocationListener {
publicabstractvoidonNewLocationReceived(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 eventpublicstaticvoidsetOnNewLocationListener(
        OnNewLocationListener listener) {
    arrOnNewLocationListener.add(listener);
}

publicstaticvoidclearOnNewLocationListener(
        OnNewLocationListener listener) {
    arrOnNewLocationListener.remove(listener);
}

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

and in one of my activity's methods:

OnNewLocationListeneronNewLocationListener=newOnNewLocationListener() {
        @OverridepublicvoidonNewLocationReceived(Location location) {
            // do something// then stop listening
            ReceiverPositioningAlarm.clearOnNewLocationListener(this);
        }
    };

    // start listening for new location
    ReceiverPositioningAlarm.setOnNewLocationListener(
            onNewLocationListener);

Solution 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.

Solution 4:

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

Post a Comment for "How Can I Send Result Data From Broadcast Receiver To Activity"