Easy Tutorial
❮ Zookeeper Watcher Verilog Number ❯

4.3.2 BroadcastReceiver Detailed Analysis

Category Android Basic Tutorial

Introduction:

>

In the previous section, we had a preliminary understanding of BroadcastReceiver, knowing two types of broadcasts: standard and ordered, dynamic or static registration of broadcast receivers, listening for system broadcasts, and sending our own broadcasts! These already meet our basic needs~ However, the broadcasts we wrote earlier are all global broadcasts! This also means that the broadcasts sent by our APP can be received by other APPs, or the broadcasts sent by other APPs can also be received by our APP, which can lead to some security issues! And Android provides us with a local broadcast mechanism, using this mechanism, the broadcast will only propagate within the APP, and the broadcast receiver can only receive broadcasts sent by the application itself!


1. Local Broadcast

1) Core Usage:

// Initialize the broadcast receiver and set the filter
localReceiver = new MyBcReceiver();
intentFilter = new IntentFilter();
intentFilter.addAction("com.jay.mybcreceiver.LOGIN_OTHER");
localBroadcastManager.registerReceiver(localReceiver, intentFilter);

Button btn_send = (Button) findViewById(R.id.btn_send);
btn_send.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent("com.jay.mybcreceiver.LOGIN_OTHER");
        localBroadcastManager.sendBroadcast(intent);
    }
});
}

@Override
protected void onDestroy() {
    super.onDestroy();
    localBroadcastManager.unregisterReceiver(localReceiver);
}
}

Alright, that's simple enough, don't forget to register the Activity!


2. Resolving the Issue of Listening to Boot-Completed Broadcast on Android 4.3 and Above:

On Android 4.3 and above, apps can be installed on an SD card. We know that the system boots up and then loads the SD card after a short delay, which might cause our app to miss the broadcast! Therefore, we need to listen for both the boot broadcast and the SD card mount broadcast!

Additionally, some phones may not have an SD card, so these two broadcasts should not be written into the same intent-filter but should be written as two separate ones. The configuration code is as follows:

<receiver android:name=".MyBroadcastReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_MOUNTED"/>
        <action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
        <data android:scheme="file"/>
    </intent-filter>
</receiver>

3. Summary of Common System Broadcasts:

Here are some system broadcasts that we might commonly use:

Intent.ACTION_AIRPLANE_MODE_CHANGED;
// Broadcast when airplane mode is turned on or off

Intent.ACTION_BATTERY_CHANGED;
// Broadcast for battery status or battery level changes
// Battery charge state or level changes, cannot be received by component declaration, only through Context.registerReceiver()

Intent.ACTION_BATTERY_LOW;
// Broadcast indicating low battery

Intent.ACTION_BATTERY_OKAY;
// Broadcast indicating the battery is okay, sent when the battery transitions from low to full

Intent.ACTION_BOOT_COMPLETED;
// Broadcast once after system boot completes

Intent.ACTION_CAMERA_BUTTON;
// Broadcast when the camera button (hardware button) is pressed

Intent.ACTION_CLOSE_SYSTEM_DIALOGS;
// Broadcast when the screen times out and locks, or when the power button is pressed, long or short press

Intent.ACTION_CONFIGURATION_CHANGED;
// Broadcast when the device's current configuration changes (including language, device orientation, etc.)

Intent.ACTION_DATE_CHANGED;
// Broadcast when the device date changes

Intent.ACTION_DEVICE_STORAGE_LOW;
// Broadcast when device memory is low, this broadcast can only be used by the system, not by other apps?

Intent.ACTION_DEVICE_STORAGE_OK;
// Broadcast when device memory becomes sufficient from being low, this broadcast can only be used by the system, not by other apps?

Intent.ACTION_DOCK_EVENT;
// Broadcast from frameworks\base\services\java\com\android\server\DockObserver.java

Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE;
// Broadcast after moving apps (app2sd) completes

Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE;
// Broadcast while moving apps (app2sd)

Intent.ACTION_GTALK_SERVICE_CONNECTED;
// Broadcast when GTalk connection is established

Intent.ACTION_GTALK_SERVICE_DISCONNECTED;
// Broadcast when GTalk connection is disconnected

Intent.ACTION_HEADSET_PLUG;
// Broadcast when a headset is plugged into the headphone jack

Intent.ACTION_INPUT_METHOD_CHANGED;
// Broadcast when the input method changes

Intent.ACTION_LOCALE_CHANGED;
// Broadcast when the device's current locale changes

Intent.ACTION_MANAGE_PACKAGE_STORAGE;
//

Intent.ACTION_MEDIA_BAD_REMOVAL;
// Broadcast when an SD card is removed improperly (correct method: Settings -- SD card and device storage -- Unmount SD card)
// Broadcast: External media has been removed from the SD card slot but not unmounted

Intent.ACTION_MEDIA_BUTTON;
// Broadcast when the "Media Button" is pressed (if there is such a hardware button)

Intent.ACTION_MEDIA_CHECKING;
// Broadcast when external storage is inserted, the system checks the SD card

Intent.ACTION_MEDIA_EJECT;
// Broadcast when external large storage is removed (e.g., SD card or external hard drive), regardless of whether it was properly unmounted
// Broadcast: User wants to remove external media (eject the card)

Intent.ACTION_MEDIA_MOUNTED;
// Broadcast when an SD card is inserted and correctly mounted
// Broadcast: External media is inserted and mounted

Intent.ACTION_MEDIA_NOFS;
//

Intent.ACTION_MEDIA_REMOVED;
// Broadcast when external storage is removed, regardless of whether it was properly unmounted
// Broadcast: External media is removed

Intent.ACTION_MEDIA_SCANNER_FINISHED;
// Broadcast: Media scanner has finished scanning a directory

Intent.ACTION_MEDIA_SCANNER_SCAN_FILE;
//

Intent.ACTION_MEDIA_SCANNER_STARTED;
// Broadcast: Media scanner has started scanning a directory

Intent.ACTION_MEDIA_SHARED;
// Broadcast: External media is unmounted because it is being shared as USB mass storage

Intent.ACTION_MEDIA_UNMOUNTABLE;
//

Intent.ACTION_MEDIA_UNMOUNTED;
// Broadcast: External media exists but is not mounted

Intent.ACTION_NEW_OUTGOING_CALL;

Intent.ACTION_PACKAGE_ADDED;
// Broadcast after a new APK is successfully installed
// Broadcast: A new application package has been installed on the device

Intent.ACTION_PACKAGE_CHANGED;
// Broadcast when an existing application package changes

Intent.ACTION_PACKAGE_DATA_CLEARED;
// Broadcast when an application's data is cleared (e.g., in Settings -- Application manager -- Select app -- Clear data)

Intent.ACTION_PACKAGE_INSTALL;
// Broadcast triggered when a download and installation completes, e.g., in the Play Store?

Intent.ACTION_PACKAGE_REMOVED;
// Broadcast after an APK is successfully uninstalled
// Broadcast: An existing application package has been removed from the device

Intent.ACTION_PACKAGE_REPLACED;
// Broadcast when an existing package is replaced (regardless of whether the new version is newer or older)

Intent.ACTION_PACKAGE_RESTARTED;
// Broadcast when a package is restarted, all processes are killed, and runtime states are removed

Intent.ACTION_POWER_CONNECTED;
// Broadcast when external power is connected

Intent.ACTION_POWER_DISCONNECTED;
// Broadcast when external power is disconnected

Intent.ACTION_PROVIDER_CHANGED;
//

Intent.ACTION_REBOOT;
// Broadcast when the device reboots

Intent.ACTION_SCREEN_OFF;
// Broadcast when the screen is turned off

Intent.ACTION_SCREEN_ON;
// Broadcast when the screen is turned on

Intent.ACTION_SHUTDOWN;
// Broadcast when the system shuts down

Intent.ACTION_TIMEZONE_CHANGED;
// Broadcast when the time zone changes

Intent.ACTION_TIME_CHANGED;
// Broadcast when the time is set

Intent.ACTION_TIME_TICK;
// Broadcast: Current time has changed (normal time passage)
// Broadcast every minute, cannot be received by component declaration, only through Context.registerReceiver()

Intent.ACTION_UID_REMOVED;
// Broadcast when a user ID is removed from the system

Intent.ACTION_UMS_CONNECTED;
// Broadcast when the device enters USB mass storage mode

Intent.ACTION_UMS_DISCONNECTED;
// Broadcast when the device exits USB mass storage mode

Intent.ACTION_USER_PRESENT;
//

Intent.ACTION_WALLPAPER_CHANGED;
// Broadcast when the device wallpaper changes

4. Summary of This Section:

-1.0 Android Basic Introduction Tutorial -1.0.1 2015 Latest Android Basic Beginner Tutorial Contents

-1.1 Background and System Architecture Analysis

-1.2 Setting Up the Development Environment

-1.2.1 Developing Android Apps with Eclipse + ADT + SDK

-1.2.2 Developing Android Apps with Android Studio

-1.3 Solving SDK Update Issues

-1.4 Installing Genymotion Emulator

-1.5.1 Basic Operations with Git Local Repository

-1.5.2 Setting Up Remote Repository with GitHub

-1.6 How to Use 9-Patch Images

-1.7 Interface Prototype Design

-1.8 Project Source Analysis (Various Files, Resource Access)

-1.9 Signing and Packaging Android Applications

-1.11 Decompiling APK to Retrieve Code & Resources

-2.1 Concepts of View and ViewGroup

-2.2.1 LinearLayout (Linear Layout)

-2.2.2 RelativeLayout (Relative Layout)

-2.2.3 TableLayout (Table Layout)

-2.2.4 FrameLayout (Frame Layout)

-2.2.5 GridLayout (Grid Layout)

-2.2.6 AbsoluteLayout (Absolute Layout)

-2.3.1 Detailed Explanation of TextView (Text Field)

-2.3.2 Detailed Explanation of EditText (Input Field)

-2.3.3 Button and ImageButton

-2.3.4 ImageView (Image View)

-2.3.5 RadioButton (Radio Button) & Checkbox (Checkbox)

-2.3.6 ToggleButton and Switch

-2.3.7 ProgressBar (Progress Bar)

-2.3.8 SeekBar (Drag Bar)

-2.3.9 RatingBar (Star Rating Bar)

-2.4.1 ScrollView (Scroll Bar)

-2.4.2 Date & Time Components (Part 1)

-2.4.3 Date & Time Components (Part 2)

-2.4.4 Basic Adapter Explanation

-2.4.5 Simple Usage of ListView

-2.4.6 Optimization of BaseAdapter

-2.4.7 Focus Issues with ListView

-2.4.8 Solving Checkbox Misalignment in ListView

-2.4.9 Data Update Issues with ListView

-2.5.0 Building a Reusable Custom BaseAdapter

-2.5.1 Implementing Multi-Layout ListView Items

-2.5.2 Basic Usage of GridView (Grid View)

-2.5.3 Basic Usage of Spinner (List Option Box)

-2.5.4 Basic Usage of AutoCompleteTextView (Auto-Complete Text Field)

-2.5.5 Basic Usage of ExpandableListView (Collapsible List)

-2.5.6 Basic Usage of ViewFlipper (Flip View)

-2.5.7 Basic Usage of Toast

-2.5.8 Detailed Explanation of Notification (Status Bar Notification)

-2.5.9 Detailed Explanation of AlertDialog (Dialog Box)

-2.6.0 Basic Usage of Other Common Dialogs

-2.6.1 Basic Usage of PopupWindow (Floating Box)

-2.6.2 Menu (Menu)

-2.6.3 Simple Usage of ViewPager

-2.6.4 Simple Usage of DrawerLayout (Official Side Menu)

-3.1.1 Event Handling Mechanism Based on Listeners

-3.2 Event Handling Mechanism Based on Callbacks

-3.3 Analysis of Handler Message Passing Mechanism

-3.4 TouchListener vs OnTouchEvent + Multi-Touch

-3.5 Listening to Content Changes in EditText

-3.6 Responding to System Setting Changes (Configuration Class)

-3.7 AsyncTask Asynchronous Tasks

-3.8 Gestures (Gestures)

-4.1.1 Beginning with Activity

-4.1.2 Intermediate with Activity

-4.1.3 Advanced with Activity

-4.2.1 Introduction to Service

-4.2.2 Advanced with Service

-4.2.3 Mastering Service

-4.3.1 Basic with BroadcastReceiver

-4.4.1 Introduction to ContentProvider

-4.4.2 Deeper Look at ContentProvider — Document Provider

-4.5.1 Basic Usage of Intent

-4.5.2 Passing Complex Data with Intent

-5.1 Basic Overview of Fragment

-5.2.1 Fragment Example Walkthrough — Bottom Navigation Bar Implementation (Method 1)

-5.2.2 Fragment Example Walkthrough — Bottom Navigation Bar Implementation (Method 2)

-5.2.3 Fragment Example Walkthrough — Bottom Navigation Bar Implementation (Method 3)

Follow on WeChat

❮ Zookeeper Watcher Verilog Number ❯