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.5 RadioButton (Radio Button) & Checkbox (Checkbox)
-2.3.6 ToggleButton and Switch
-2.3.7 ProgressBar (Progress 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.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.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
-4.1.1 Beginning with Activity
-4.1.2 Intermediate with Activity
-4.2.1 Introduction to Service
-4.3.1 Basic with BroadcastReceiver
- 4.3.2 In-Depth with BroadcastReceiver
-4.4.1 Introduction to ContentProvider
-4.4.2 Deeper Look at ContentProvider — Document Provider
-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)
- 5.2.4 Fragment Example Walkthrough — Bottom Navigation Bar + ViewPager Swipe to Switch Pages
- 5.2.5 Fragment Example Walkthrough — Simple Implementation of News (Shopping) App List Fragment
- 6.1 Data Storage and Access — File Storage and Reading/Writing
- 6.2 Data Storage and Access — SharedPreferences for Saving User Preferences
- 6.3.1 Data Storage and Access — Introduction to SQLite Database
- 6.3.2 Data Storage and Access — Further Exploration of SQLite Database
- 7.1.1 Android Network Programming Essentials and Learning HTTP Protocol
- 7.1.2 Learning Android HTTP Request Headers and Response Headers
- 7.1.3 Android HTTP Request Methods: HttpURLConnection
- 7.1.4 Android HTTP Request Methods: HttpClient
- 7.2.1 Android XML Data Parsing
- 7.2.2 Android JSON Data Parsing
- 7.3.1 Android File Upload
- 7.3.2 Android File Download (1)
- 7.3.3 Android File Download (2)
- 7.4 Android Calling WebService
- 7.5.1 WebView (Web View) Basic Usage
- 7.5.2 WebView and JavaScript Interaction Basics
- 7.5.3 Important Considerations for WebView in Android 4.4 and Later
- 7.5.4 WebView File Download
- 7.5.5 WebView Cache Issues
- 7.5.6 WebView Handling Webpage Error Code Information
- 7.6.1 Socket Learning Network Basics Preparation
- 7.6.2 Socket Communication Based on TCP Protocol (1)
- 7.6.3 Socket Communication Based on TCP Protocol (2)
- 7.6.4 Socket Communication Based on UDP Protocol
- 8.1.1 Summary of 13 Drawable Types in Android Part 1
- 8.1.2 Summary of 13 Drawable Types in Android Part 2
- 8.1.3 Summary of 13 Drawable Types in Android Part 3
- 8.2.1 Bitmap (Bitmap) Comprehensive Analysis Part 1
- 8.2.2 OOM Issues Caused by Bitmap
- 8.3.1 Detailed Explanation of Three Drawing Tool Classes
- 8.3.2 Practical Examples of Drawing Classes
- 8.3.3 Paint API — MaskFilter (Mask)
- 8.3.4 Paint API — Xfermode and PorterDuff Detailed Explanation (Part 1)
- 8.3.5 Paint API — Xfermode and PorterDuff Detailed Explanation (Part 2)
- 8.3.6 Paint API — Xfermode and PorterDuff Detailed Explanation (Part 3)
- 8.3.7 Paint API — Xfermode and PorterDuff Detailed Explanation (Part 4)
- 8.3.8 Paint API — Xfermode and PorterDuff Detailed Explanation (Part 5)
- 8.3.9 Paint API — ColorFilter (Color Filter) (1/3)
- 8.3.10 Paint API — ColorFilter (Color Filter) (2/3)
- 8.3.11 Paint API — ColorFilter (Color Filter) (3/3)
- 8.3.12 Paint API — PathEffect (Path Effect)
- 8.3.13 Paint API — Shader (Image Rendering)
- 8.3.14 Paint Enumerations/Constants and ShadowLayer Shadow Effects
- 8.3.15 Paint API — Typeface (Font Style)
- 8.3.16 Canvas API Detailed Explanation (Part 1)
- 8.3.17 Canvas API Detailed Explanation (Part 2) Clipping Methods Collection
- 8.3.18 Canvas API Detailed Explanation (Part 3) Matrix and drawBitmapMesh
- 8.4.1 Android Animation Collection — Frame Animation
- 8.4.2 Android Animation Collection — Tween Animation
- 8.4.3 Android Animation Collection — Property Animation - Introduction
- 8.4.4 Android Animation Collection — Property Animation - Further Exploration
- 9.1 Using SoundPool to Play Sound Effects (Duang~)
- 9.2 MediaPlayer for Audio and Video Playback
- 9.3 Using Camera to Take Photos
- 9.4 Using MediaRecord for Audio Recording
- 10.1 TelephonyManager (Telephony Manager)
- 10.2 SmsManager (SMS Manager)
- 10.3 AudioManager (Audio Manager)
- 10.4 Vibrator (Vibrator)
- 10.5 AlarmManager (Alarm Service)
- 10.6 PowerManager (Power Service)
- 10.7 WindowManager (Window Management Service)
- 10.8 LayoutInflater (Layout Service)
- 10.9 WallpaperManager (Wallpaper Manager)
- 10.10 Sensor Topics (1) — Introduction
- 10.11 Sensor Topics (2) — Orientation Sensor
- 10.12 Sensor Topics (3) — Accelerometer/Gyroscope Sensor
- 10.12 Sensor Topics (4) — Understanding Other Sensors
- 10.14 Android GPS Introduction
- 11.0 "2015 Latest Android Basic Beginner's Guide" Completion Celebration
- 12.1 Android Practice: DrySister Viewing Girls App (First Edition) — Project Setup and Simple Implementation
- 12.2 DrySister Viewing Girls App (First Edition) — 2. Parsing Backend Data
- 12.3 DrySister View Girls App (Version 1) – 3. Image Loading Optimization (Writing a Small Image Cache Framework)
12.4 DrySister View Girls App (Version 1) – 4. Adding Data Caching (Integrating SQLite)
12.5 DrySister View Girls App (Version 1) – 5. Code Review, Adjustments, and Logging Class Writing