Disable the Volume Toast - android

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.

Related

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?

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);
}

Volume preference - blocking volume keys

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

Problem with default Android onKey... sound

I'm having hard time figuring out how to get rid of the default sound played while any key is pressed. I'm using following method but it still plays the same "beep" sound.
#Override public boolean onKeyDown( int keyCode, KeyEvent event )
{
switch ( keyCode )
{
case 25:
ChangeImageUP( );
break;
case 24:
ChangeImageDOWN( );
break;
default:
return super.onKeyDown( keyCode, event );
}
return true;
}
Please help.
// Update
I have figure out that if I overwrite onKeyUp in my main activity the beep is gone. But when i start a second activity the beep returns, even after using that onKeyUp method.
That is controlled by the operating system and the users settings. You might not be able to modify it..

android - overridden volume button has affected back button?

Using the code below I have stopped the use of the volume buttons unless I am streaming audio (otherwise it annoyingly changes the ringer volume), but the 'Back' button isn't working.
Pressing 'back' should got to my phones desktop (or exit my app, like you would expect), but it isn't doing anything. If I open the menu, 'Back' will close the menu as it should, but I can't leave the app.
I have copied the code onto other activities within my app, if I open another activity within my app, because the 'Back' button isn't working, I can't go back to the main screen :)
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//Suppress the use of the volume keys unless we are currently listening to the stream
if(keyCode==KeyEvent.KEYCODE_VOLUME_UP) {
if(StreamService.INT_PLAY_STATE==0){
return true;
}else{
return false;
}
}
if(keyCode==KeyEvent.KEYCODE_VOLUME_DOWN) {
if(StreamService.INT_PLAY_STATE==0){
return true;
}else{
return false;
}
}
return false;
Why is this happening?
Haven't tested, but I think you need to include an else where you call super.onKeyDown, ie:
if(keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
code
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
more code
} else {
super.onKeyDown(keyCode, event);
}
Otherwise, you're capturing all keycodes and returning false after checking the volume codes.
A simpler and more robust way to have the volume keys always control the Media volume is to insert this line into your Activity's onCreate():
setVolumeControlStream(AudioManager.STREAM_MUSIC);
Dude, just change the audio context on that activity to media volume:
http://developer.android.com/reference/android/media/AudioManager.html
EDIT:
private AudioManager audio;
Inside onCreate:
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Override onKeyDown:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
return true;
default:
return false;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
//your code
return true;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
//your code
return true;
} else {
super.onKeyDown(keyCode, event);
}
return true;
}

Categories

Resources