Volume preference - blocking volume keys - android

I have seen couple of similar problems with solutions, but I couldn't find one that would work in my situation.
I am making VolumePreference (extending DialogPreference) that let's user choose volume level for some alarm.
In other preference user chooses desired ringtone that is played during alarm. It is also played while user is choosing volume in VolumePreference, so he knows how it actually sounds.
In yet another preference user chooses if alarm should "override" phone's media volume level when playing - I do that, so if user wants to have fixed volume level for alarm, then it shouldn't be affected by changes made by volume keys and so on.
If user chooses to do that, before starting to play alarm in AlarmActivity, I set volume to max level with AudioManager and intercept all keyDown events of volume keys, restoring volume level after alarm finishes.
Problem is, I can't block volume keys within my VolumePreference as there is no onKeyDown method.
After some checking, I found registerMediaButtonEventReceiver method of AudioManager that "Register a component to be the sole receiver of MEDIA_BUTTON intents.", which I believe could help in my situation (making some empty receiver), and even make volume locking more universal (register when I want to start lock, unregister after unlock), but it is working from API8, while I am making app for API7 - which still hold over 10% of market from what I read, so I would like to stick to it.
Any ideas on how one could block volume changes in PreferenceDialog?

After some thinking solution proved to be really simple - one can override onKeyDown method of View created in onCreateDialogView of DialogPreference or set onKeyListener of that View.
First example:
LinearLayout layout = new LinearLayout(mContext)
{
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
|| keyCode == KeyEvent.KEYCODE_VOLUME_UP) return true;
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
|| keyCode == KeyEvent.KEYCODE_VOLUME_UP) return true;
return super.onKeyUp(keyCode, event);
}
};
Second example (mDialogView is saved reference to layout from first example):
mDialogView.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
|| keyCode == KeyEvent.KEYCODE_VOLUME_UP)
return true;
return false;
}
});
I have chosen second solution, as I think it is more flexible for two reasons at least
I can just remove listener to stop blocking volume keys, while in first method I can't
in first solution I need to decide if I want to block keys input while creating View - not much use if VolumePreference is extending some other Preference that shouldn't block keys input

Related

Android MediaPlayer to mute on a single click on Volume down botton pressed

I am using Android's MediaPlayer in my RingtoneService (which is a background Service for Ringtone in my application)to ring a ringtone when my Alarm goes on and off!
on the start of Ringtone Service for alarming
MediaPlayer mediaSong;
mediaSong = MediaPlayer.create(this, R.raw.xyz);
mediaSong.start();
on stop after some time
mediaSong.stop();
Its perfect in ringing and stopping the tone.whereas, I would like to make my device silent or say mute when I click on hardware's volume button pressed when an alarm is still ringing.
some stated solutions didn't answer my requirement.Any help is appreciated, please.
Please let me know somewhere if I am unclear!
Thanks in advance!
You need to override onKeyDown method in your Activity.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
mediaSong.stop
return true;
}
return super.onKeyDown(keyCode, event);
}

Why does my webview finish unexpectedly?

I place a fragment with a webview widget to load my webpages and create a host fragmentActivity as my webpages' container. Now that there is a page, including a media player, seems like to support various of content for us. Here comes the problem, if I click the play button, the media player works well itself, however, when i adjust the power of volume, either higher or lower, the host activity finish unexpectedly. Complementally, I just handle the back event in the method of onKeyEvent.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if(webview.canGoBack()){
webview.goBack();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
Anybody helps me?

Detect event when hardware volume button is pressed in android

i am developing an app which listen volume events whenever hardware volume button is pressed. app can be in foreground or background. i have followed [this] (Is there a broadcast action for volume changes?) but its not working correctly. following issues i am facing
1) If I press the volume button once - the event is triggered 4-6 times.
2) If current volume is maximum and i increase the volume then event doesn't fire..
Please help me.
Try to use following code -
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){
//Do something
}
return true;
}
for background - any way to detect volume key presses or volume changes with android service?
On a rooted device, you can use the Xposed framework to hook yourself into PhoneWindowManager. A good example is the Xposed Torch Module. Just decompile it and see how they do things.
You can detect this by polling AudioManager for current volume, it's not nice solution, but i don't know better.
private Handler handler;
public int volume;
private Runnable volumeUpdater = new Runnable() {
private int updatesInterval = 100;
#Override
public void run() {
if(volume != audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)){
//volume changed put logic here
}
handler.postDelayed(volumeUpdater, updatesInterval);
}
};

Intercept long media button press and disable Google Now

I wish to start a reciever\service that recieves the long press from the headset button and disables the Google Now voice recognition feature. Is that possible or is the Google Now response hard-coded to be in higher priority prior to Jelly Bean?
Thanks!
This is how I have implemented it listening to LongPress of volume key.
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
{
// do something here
return true;
}
return super.onKeyLongPress(keyCode, event);
}

Disable the Volume Toast

I'm trying to increase the volume using AudioManager.
But it showing Native Android Volume UI(Volume Seekbar) toast.I want to disable this.
I know this can be possible in Activity using Key Event but i want to do it through service.
How to disable the toast?
Here is a screenshot of the toast:
The AudioManager-class offers the following methods to adjust the volume of certain streams:
adjustVolume(int, int)
adjustStreamVolume(int, int, int)
adjustSuggestedStreamVolume(int, int, int)
setStreamVolume(int, int, int)
All those methods take a flag-parameter as their last argument. The flag which is interesting for you is the FLAG_SHOW_UI which:
Show a toast containing the current volume.
So, to get rid of the Toast, don't supply this flag (or the int-value 1) but supply all your other flags (if needed) or just 0:
AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
manager.adjustVolume(AudioManager.ADJUST_RAISE, 0);
The above code-snippet works for me on Android 4.0.4 (Motorola Xoom) and Android 2.3.5 (HTC Desire HD).
I know it's a little bit late, but I think I've managed to hide the volume toast using this:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch(keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
//volumeDown();
return true;
case KeyEvent.KEYCODE_VOLUME_UP:
//volumeUp();
return true;
}
return super.onKeyUp(keyCode, event);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch(keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
volumeDown();
return true;
case KeyEvent.KEYCODE_VOLUME_UP:
volumeUp();
return true;
}
return super.onKeyDown(keyCode, event);
}
private void volumeUp(){
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, SOME_RANDOM_VALUE, AudioManager.FLAG_VIBRATE);
}
Ovveriding only one method from onKeyUp and onKeyDown didn't prevent displaying the toast (even when using flags).
It's a little bit weird - like Android tries to display the toast twice.

Categories

Resources