Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use a callback method from a BroadcastReceiver?

Tags:

android

Learning to use the BroadcastReceiver class in Android, I have written a small program to receive the battery charge state and write it to three TextView fields in an activity.

However, I have made the BroadcastReceiver as a separate class to make it more simple and separate from the activity. Therefore I have to find a method to tell my Activity class that the battery data has been updated, or, which is my solution, to pass in references to the TextView fields from the Activity to the BroadcastReceiver class.

Does anyone know whether it is possible to make a callback method from the BroadcastReceiver to start a function, f.ex. updateTextViews(); in the Activity?

Here is the source code - note there are two java files: http://pastebin.com/qjCTsSuH

Regards, Niels.

like image 550
skovmand Avatar asked Mar 01 '14 14:03

skovmand


People also ask

How do I send data from BroadcastReceiver to activity?

startActivity(i); Then, in your activity, you will need to getExtra as so: Intent intent = getIntent(); String message = intent. getStringExtra("message");

When would you use a BroadcastReceiver?

Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events.

What is the method name in BroadcastReceiver receive the message?

onReceive. This method is called when the BroadcastReceiver is receiving an Intent broadcast.

Which intent is used by BroadcastReceiver?

Creating a BroadcastReceiverThe intent object is passed with all the additional data. A Context object is also available and is used to start an activity or service using context. startActivity(myIntent); or context. startService(myService); respectively.


1 Answers

What worked a charm for me is simply declaring the interface objects as static. Bear in mind though that statics can cause as many problems as they solve as statics persist therir values accross instances.

public class MainActivity extends AppCompatActivity implements SocketMessageReceiver.ISocketMessageReceiver {

    //Declare the cb interface static in your activity
    private static SocketMessageReceiver.ISocketMessageReceiver iSocketMessageReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        //Assign this
        iSocketMessageReceiver = this;
        socketMessageReceiver.registerCallback(iSocketMessageReceiver);
    }

    @Override
    public void sendSocketMessage(String socketMessage) {
        lblEchoMessage.setText(socketMessage);
    }

}

And in your Receiver ....

public class SocketMessageReceiver extends BroadcastReceiver {

    interface ISocketMessageReceiver {
        void sendSocketMessage(String socketMessage);
    }

    //Also declare the interface in your BroadcastReceiver as static
    private static ISocketMessageReceiver iSocketMessageReceiver;

    @Override
    public void onReceive(Context context, Intent intent) {

        if(intent.getAction().equals("com.WarwickWestonWright.SocketExample.RECEIVE")) {
            iSocketMessageReceiver.sendSocketMessage(intent.getBundleExtra("DATA").getString("DATA"));
        }

    }

    public void registerCallback(ISocketMessageReceiver iSocketMessageReceiver) {
        this.iSocketMessageReceiver = iSocketMessageReceiver;
    }

}
like image 158
user2288580 Avatar answered Sep 20 '22 01:09

user2288580