[Android] How to detect volume change (= volume button press)

It's a legacy method that has been around for a long time, but I'll write it because the information that comes out by google is a little impractical.

Method

There is AudioManager.VOLUME_CHANGED_ACTION (android.media.VOLUME_CHANGED_ACTION) as an Intent that detects when the volume is changed or the volume button is pressed. All you have to do is receive this with the Broadcast Receiver.

Up to this point, the information that comes out immediately after google, but AudioManager.VOLUME_CHANGED_ACTION needs to be aware of the following points.

--Multiple events are notified by one volume change operation --AudioManager.STREAM_MUSIC, etc. Notified for each STREAM, and even if the volume does not change --The volume UI is displayed when the first button is operated while the screen is ON, and the event is not notified because the volume does not actually change. --When the screen is off, the event will not be notified even if you press the button unless you are playing music with a music app, etc. --Because the volume does not change

Source code

AndroidManifest.xml


<receiver android:name=".VolumeChangedActionReceiver">
    <intent-filter>
        <actionandroid:name="android.media.VOLUME_CHANGED_ACTION" />
    </intent-filter>
</receiver>

VolumeChangedActionReceiver.java


public static class VolumeChangedActionReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals("android.media.VOLUME_CHANGED_ACTION")) {
            int newVolume = intent.getIntExtra("android.media.EXTRA_VOLUME_STREAM_VALUE", 0);
            int oldVolume = intent.getIntExtra("android.media.EXTRA_PREV_VOLUME_STREAM_VALUE", 0);
            int streamType = intent.getIntExtra("android.media.EXTRA_VOLUME_STREAM_TYPE", 0);

            if (streamType == AudioManager.STREAM_MUSIC) {
                // TODO: write your code
            }
        }
    }
}

Recommended Posts

[Android] How to detect volume change (= volume button press)
How to detect microphone conflicts on Android
Android Easily give a "press" to a button
[Android Studio] How to change TextView to any font [Java]
How to "hollow" View on Android
How to change kube-proxy to ipvs mode.
[Android] How to make Dialog Fragment
How to change from HTML to Haml
How to change app name in rails
How to use named volume in docker-compose.yml
[Android] How to deal with dark themes
How to change the timezone on Ubuntu
Ransack sort_link How to change the color!
How to use ExpandableListView in Android Studio
[Android] How to convert a character string to resourceId
How to change Finder's default text editor to Atom
How to make Unity Native Plugin (Android version)
[Rails] How to create a Twitter share button
http: // localhost: How to change the port number
How to write React Native bridge ~ Android version ~
How to make an crazy Android music player
How to deploy
How to change the action with multiple submit buttons
[Android / Java] Set up a button to return to Fragment
[Ruby on Rails] How to change the column name
[Android Studio] [Java] How to fix the screen vertically
[Rails] How to change the column name of the table
How to use Truth (assertion library for Java / Android)
How to change from Oracle Java 8 to Adopt Open JDK 9
[Android] How to get the setting language of the terminal
How to prevent editTextPreference of android PreferenceFragmentCompat from breaking
[Beginner] How to use devise Change settings from introduction
How to enable submit button every time with jquery
[Linux] Easy commentary! How to check and change permissions
How to set up Android OR mapper "Orma" (Kotlin)
How to perform a specific process when the back button is pressed in Android Fragment