Is there a broadcast action for volume changes?
There is no broadcast action, but I did find you can hook up a content observer to get notified when the settings change, volume of streams being some of those settings. Register for the android.provider.Settings.System.CONTENT_URI to be notified of all settings changes:
mSettingsContentObserver = new SettingsContentObserver( new Handler() );
this.getApplicationContext().getContentResolver().registerContentObserver(
android.provider.Settings.System.CONTENT_URI, true,
mSettingsContentObserver );
The content observer might look something like this:
public class SettingsContentObserver extends ContentObserver {
public SettingsContentObserver(Handler handler) {
super(handler);
}
@Override
public boolean deliverSelfNotifications() {
return super.deliverSelfNotifications();
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.v(LOG_TAG, "Settings change detected");
updateStuff();
}
}
And be sure to unregister the content observer at some point.
Volume change Listener: Is registerMediaButtonEventReceiver preferable to onKeyDown?
It would be great to have in future APIs a BroadcastReceiver
for volume streams, but today maybe the best solution is to register a ContentObserver
for the settings (that includes VOLUME_NOTIFICATION):
mSettingsContentObserver = new SettingsContentObserver( new Handler() );
this.getApplicationContext().getContentResolver().registerContentObserver(
android.provider.Settings.System.CONTENT_URI, true,
mSettingsContentObserver );
see this answer for more info: https://stackoverflow.com/a/7017516/117382
Edit: Corrected with working code. Maybe this solution is better: https://stackoverflow.com/a/17398781/117382
My broadcast receiver class not responding for volume button press?
I try to code what you want and it's work.
1) I defined my BroacastReceiver in manifest like that :
...
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.test.CallBroadcastReceiver" >
<intent-filter>
<action android:name="android.media.VOLUME_CHANGED_ACTION" />
</intent-filter>
</receiver>
</application>
...
2) I created CallBroadcastReceiver
:
package com.example.test;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class CallBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int volume = (Integer)intent.getExtras().get("android.media.EXTRA_VOLUME_STREAM_VALUE");
Log.i("Tag", "Action : "+ intent.getAction() + " / volume : "+volume);
}
}
You don't need to check the value of the Action of your intent because your BroadcastReceiver just listened VOLUME_CHANGED_ACTION
. But if you checked the value of your action it's android.media.VOLUME_CHANGED_ACTION
.
After, I try this app in my Nexus 6, it's possible that the problem comes from the phone.
Android BroadCastReceiver for volume key up and down
The intent does not have an EXTRA_KEY_EVENT extra. It does have an extra android.media.EXTRA_VOLUME_STREAM_VALUE which contains the new volume.
int volume = (Integer)intent.getExtras().get("android.media.EXTRA_VOLUME_STREAM_VALUE");
If you store the old value, you can infer whether volume up or down was pressed.
Volume change listener?
Check out registerMediaButtonEventReceiver(ComponentName broadcastReceiver);
Define a BroadcastReceiver
that handles ACTION_MEDIA_BUTTON
. The recieved intent
includes a single extra field, EXTRA_KEY_EVENT
, containing the key event that caused the broadcast. You can use this key event to get which key was pressed.
EDIT:
This is just a sample code. syntax errors may be there.
// in onCreate of activity
registerMediaButtonEventReceiver(mediaReceiver );
// later somewhere in activity.
MediaButton_Receiver mediaReceiver = new MediaButton_Receiver();
class MediaButton_Receiver implements BroadcastReceiver {
void onReceive(Intent intent) {
KeyEvent ke = (KeyEvent)intent.getExtra(Intent.EXTRA_KEY_EVENT);
if (ke .getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {
}
// Similarly other key codes .......
}
}
Unregister the receiver in onPause()
or onStop()
How to receive volume changed events for the voice (in-call) stream type?
My temporary (and probably bad) workaround is to catch the ArrayIndexOutOfBoundsException
, and in the catch block to test the stream type to have value of Settings.System.VOLUME_SETTINGS.length
.
This is the best I have so far. I don't know if it might catch other stream types as well, and also if it is going to work on other Android versions than 2.3.3-2.3.5.
Related Topics
How to Handle Dynamic Json in Retrofit
Noclassdeffounderror For Code in an Java Library on Android
How to Detect Android App Uninstall
Programmatically Switching Off Android Phone
How to Get Current Time from Internet in Android
What Android Tools and Methods Work Best to Find Memory/Resource Leaks
How to Check If Alarmmanager Already Has an Alarm Set
How to Add an Array or Object to Sharedpreferences on Android
How to Disable Bottomnavigationview Shift Mode
How to Make Wrap_Content Work on a Recyclerview
How to Refresh Mediastore on Android
How to Open the "Front Camera" on the Android Platform
How to Disable Landscape Mode in Android
Httpclient Won't Import in Android Studio
Playing an Arbitrary Tone With Android
Android and Facebook Share Intent
More Than One File Was Found with Os Independent Path 'Meta-Inf/License'