Android.content.BroadcastReceiver
lastmod 2024-11-29created 2016-05-195 edits
Android apps can send or receive broadcast messages from the Android system, and for other Android apps it is a publish-subscribe design pattern. These broadcasts are sent when an event of interest occurs. For example, the Android system sends broadcasts when various system events occur. can occur. Apps can also send custom broadcasts to other apps, content they might be interested in (e.g., some new data has been downloaded).
The system optimizes broadcast delivery to optimal system health therefore broadcast delivery time is guaranteed apps that need low-latency inter-process communication consider bound services.
Apps can register to receive specific broadcasts. When a broadcast is sent, the system automatically the broadcast can receive a specific type of broadcast.
In general, broadcasts can be used as a messaging system across apps. outside the normal user flow. However, you must be careful not to abuse it. respond to broadcasts and can affect system performance degradation
Examples
- SMS-Forward - Forwards incoming text messages to another phone, Telegram, or a webhook.
How to detect volume change Events (Broadcast)
Volume changes can be detected with a broadcast (Broadcast) as follows.
class MediaVolumeChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int volume = (Integer)intent.getExtras().get("android.media.EXTRA_VOLUME_STREAM_VALUE");
}
}
// ...
MediaVolumeChangeReceiver _recv = new MediaVolumeChangeReceiver();
// ...
Context context = getApplicationContext();
IntentFilter filter = new IntentFilter();
filter.addAction("android.media.VOLUME_CHANGED_ACTION");
context.registerReceiver(recv, filter);
SCREEN_OFF Volume Event
Issue with detecting volume changes while the screen is off (SCREEN_OFF):
No, This is not possible usually because broadcast receiver not work when screen light off because after screen off your volume button get lock and volume level not change.
Alternative Way to do this:
But you can achieve using run Media Player with zero sound in background infinite times that keep your volume button active always or you can use Wake Lock to achieve this.
But I don't suggest to do this method of long time because it totally drain the battery very fast. You can use these alternative method to do this for short period of time otherwise mobile battery get down fastly.
In other words, android.media.MediaPlayer must be run indefinitely at volume 0. The battery drain issue must be reviewed separately.
How to detect screen (SCREEN) ON/OFF Events (Broadcast)
Events where the screen turns off and on can be detected with a broadcast (Broadcast) as follows.
class ScreenEventReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Screen on/off.
}
}
// ...
ScreenEventReceiver _recv = new ScreenEventReceiver();
// ...
Context context = getApplicationContext();
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.SCREEN_ON");
filter.addAction("android.intent.action.SCREEN_OFF");
context.registerReceiver(recv, filter);
Insert the following code inside the application tag of AndroidManifest.xml.
<receiver android:name=".ChangeReceiver" >
<intent-filter>
<action android:name="android.intent.action.ACTION_SCREEN_ON" />
<action android:name="android.intent.action.ACTION_SCREEN_OFF" />
</intent-filter>
</receiver>
Favorite site
- An explanation of Broadcast Receiver 1
- Android Tech: BroadcastReceiver
- A collection of commonly used Android Broadcast Action names
- Stackoverflow: Android - Is there a broadcast action for volume changes?
- Detecting the Headset button (ACTION_MEDIA_BUTTON)
- Broadcasts sent by the system
- What action is fired when switching between ring/vibrate/silent?
- How an Activity can check when volume is set while a System popup is showing
- Control Audio in Android (lesson 16)
- Android Event handling mechanism 2
- [Recommended] Why the Home key and Power (Endcall) Key cannot be detected with onKeyDown and onKeyUp on Android
- Android Broadcast/BroadcastReceiver
- Handling ACTION_MEDIA_BUTTON BroadcastReceiver registration on Android ICS
- Intent actions related to Android broadcast receivers 3
- Android, BroadcastReceiver, headset controls
- Capture media button on Android >=4.0 (works on 2.3)
- Android BroadCastReceiver for volume key up and down