What happens when a user holds the headset button - java

I'm trying to find out what happens when a user pushes the media button of headset. I know the phone recognizes holding the button, but when I test it myself and hold the button broadcast will send a ACTION_MEDIA_BUTTON. When I push the button down and release it, I get two broadcasts (one for ACTION_DOWN and one for ACTION_UP). But when I hold it doesn't send anything.

This is how most buttons work for anything. Almost no hardware will tell you when something is being pushed down, just when a change is made.
If you want to know how long a button is held down for, you want to start recording the time using something like System.currentTimeMillis when you receive an ACTION_DOWN message and when you receive an ACTION_UP you can record the end time and then calculate the difference.

Related

Android: how to stop listener when app closes

When my app starts I start a location listener, I'd like to turn it off when the user exits the app, whether by pressing the back button or just putting it in the background, and I would like to resume listening when the app is back.
I couldn't find onPause/onResume for the app level, so I thought about holding a counter that will count the number of active activities and if it's 0 it will shut down the listener, but there must be a better way...
Please advise.
To detect if your app is running in background you can read this article:
http://www.vardhan-ds.co.in/2013/05/android-solution-to-detect-when-android.html
And to shut down your listener, you just need to remove the Listener from your manager.
If you need some help with this I can post some code, but I think this description will help you.

Android Volume Button Listener when Screen Off

I have an activity listening for volume button events using onKeyDown(), however I am not able to see these events when the screen is off.
Is there any way I can still listen for volume button presses when the screen is off?
No, This is not possible usually because broadcast receiver not work when screen light off because after screen off your volume button get lock and volume level not change.
Alternative Way to do this:
But you can achieve using run Media Player with zero sound in background infinite times that keep your volume button active always or you can use Wake Lock to achieve this.
But I don't suggest to do this method of long time because it totally drain the battery very fast. You can use these alternative method to do this for short period of time otherwise mobile battery get down fastly.
If you wish to control the volume of audio played through your app, make use of setVolumeControlStream() method. It directs volume key presses to the audio stream you specify.
setVolumeControlStream(AudioManager.STREAM_MUSIC);
However, if you wish to make the volume button a shortcut key for your app, I'm afraid there is no straightforward solution.

Close notification event in Vaadin Window

I am writing an application in Vaadin that searches for results. A form is used to specify the criteria and a table is used to represent the results.
I also want to show the amount of results found in a Window.Notification of Vaadin.
My problem is that I want the notification to stay there until the user clicks on it so it does not disappear after a few seconds.
If you just enter a large delay time, then if you don't click on these notifications and do several searches, all result notifications will come over each other. So if you then click on them to remove them, you will see the amount of results from your previous search.
This is what I want to avoid.
Notifications are added to the Window and as far as I can see, you can't get a reference to them to update them. So you can't change the current notification if there is already one present. After it is painted it is removed from the LinkedList<Notification> notifications of com.vaadin.ui.Window
One of the things I was looking at is how Vaadin closes the notification after you click on it so that maybe I can do the same before each search so that the previous notification is removed and the new search adds a new notification.
But so far I am unable to find how it is done.
A notification in html is shown as a div. So maybe there is a javascript I could call in Vaadin to remove that div?
Does anyone know how the timer works for closing the notification after the defined delay? Will this call a javascript that I could also call?
If the delay on the notification is -1, it has to be clicked on by the user. Unless the notification is of type TYPE_ERROR_MESSAGE, you have to create a notification object yourself:
Notification message = new Notification("Message", Notification.TYPE_HUMANIZED_MESSAGE);
message.setDelayMsec(-1);
getWindow().showNotification(message);
There is no mechanism in Vaadin to listen for hide events of notifications. You could create your own widget derived from VNotification to transmit this condition back to the server.

How to make a protection of many times pressing the button?

In my app I use a camera, and I want to take a pictures. In my app is button (Photo). If I press it one times - all work perfect, but if I press button many times until camera take picture, my app hangs. How can I fix it?
In your onClickListener call Button.setEnabled() and set it to false.
Then set it to true when you've finished taking the photo.
Use a listener that disables the button on press (setEnalbed(false)), than start a countdown thread that re-enables it after some time, 200ms maybe, or whatever fits the best.
After second thoughts this might not be a really good idea.
There is a chance that the thread will not be scheduled to run, so if you exactly know the point when you can re-enable the button in your code, don't use threads.

Android - other app stealing my intent receiver?

I have an app which uses the ACTION_MEDIA_BUTTON intent with a BroadcastReceiver to control a music player. The user pushes a button on external hardware and it controls the in-app music player. The user can also HOLD DOWN the button and change the volume.
I recently downloaded another app which uses the headset button, and it takes over the media button intent from my app! So when this other app is open and I press the button, the other app will start running, but my app will think that the button is still pressed down so it will cycle the volume.
To summarize,
my app is open, supposed to be sole listener of media button intents
other app gets opened, it also wants to be sole listener of media button intents
button gets pressed with both apps open, control goes to other app
my app thinks the button is being held down, as it lost control as the button was pressed in down mode (I think). It then launches functions I don't want launched because it thinks the user has held down the button.
Is there any way I could make sure that while my app is open it's the sole receiver of this media button intent? Could I at least check to see if another app has taken over, so I can prevent unexpected behaviour?
Thank you for any help, I've never had apps not play nicely before!
You can alter your BroadcastReceiver's priority (make it something large, like 10000): it should then get the Intent first, and then you can pass it on to the other app.
I have a similiar issue. I believe, outside of the 'arms race' over the priorites mentioned, the only real solution is to close the other application. If you are releasing this application to other users, you could possibly give them a message telling them to close other media player apps and services.
In your manifest you can set the intent priority to the max value of an integer which is: 2147483647.
You should not however set your IntentFilter priority over 1000 as it tells you in the API docs. You can set the IntentFilter priority like so:
myIntentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
I assume you should subtract one from the SYSTEM_HIGH_PRIORITY constant as the docs say the value must be less than SYSTEM_HIGH_PRIORITY.
Quote from docs about IntentFilter.setPriority(int):
Applications must use a value that is larger than SYSTEM_LOW_PRIORITY and smaller than SYSTEM_HIGH_PRIORITY.

Categories