Android Broadcast Receiver Concepts and Sample App
/This video, part of my Android Quick Code Access series of posts, explains Android Broadcast Receivers. It's from my course Learning Android App Programming published by InfiniteSkills.
Here's the Java code for the example...
import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.widget.Toast; // Declare main activity for the application. public class Main extends Activity { // Instantiate BroadcastReceiver. private BroadcastReceiver receiver = new BroadcastReceiver(){ // Define actions to be taken when broadcast is received. @Override public void onReceive(Context c, Intent i) { // Do the work of the BroadcastReceiver. // In this example, a message is toasted to the user. Toast.makeText(getBaseContext(), "ACTION_TIME_TICK intent received.", Toast.LENGTH_LONG) .show(); } }; // Initialize the user interface in the onCreate method // of the main activity. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } // Register the receiver with its filter. @Override protected void onResume() { this.registerReceiver(receiver, filter); super.onResume(); } // Unregister the receiver when the application is paused. @Override protected void onPause() { this.unregisterReceiver(receiver); super.onPause(); } }