Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 715
Bobs Avatar asked Jul 12 '11 09:07

Bobs


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

like image 104
user1853984 Avatar answered Oct 19 '22 02:10

user1853984