Android Broadcastreceiver for Volume Key Up and Down

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.

Android capturing volume up/down key presses in broadcast receiver?

as well as the screen on/off button - is this possible?

Fortunately, no.

But I would like it to be possible even when the application is not open, hence why I'd like to set up a broadcast receiver or maybe stick something in a service to make this possible.

This is not possible for the volume buttons.

For example, AndroSS allows you to override the camera hardware button to take a screenshot.

That is the camera button. If the foreground activity does not consume a camera button click, that gets turned into a broadcast Intent, which other applications can listen for. This behavior is unique to the camera and media buttons and is not used with any other hardware buttons.

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.

Receive long volume-key press

Cyanogen sends an MEDIA_NEXT event on long press of volume up. (at least Cyanogemod 11 did this on my phone)

You should be able to test this easily. Just listen to MEDIA_NEXT and check if you receive it on long press.

To edit 1:
See BroadcastReceiver for ACTION_MEDIA_BUTTON not working

(also: please open a new question instead of editing an old one if you have a new problem)



Related Topics



Leave a reply



Submit