add broad cast receiver to volume button android - android

I want to make broadcastreceiver to volume button in android.
when clicking up and down to in 3 seconds to open my aapplication.
is it possible ? if yes i will be happy to get the broadcastcode for this?

There is no BroadcastReceiver for volume up or down button presently.
Alternate may be a service but it is not recommended to run a service all the time since that consumes both battery and memory.

Related

Start app by hardwarekey Android Application

I just started creating apps, and now I want to know how to build something that
helps me to start an app, by execercising a volume down/up press during the sleepmode of my phone.
I think that it will need an OnReceive Method or something that works like a onclicklistener but i cant figure it out.
Greetings
In a normal way its not possible to catch your volume down/up receiver when your phone is locked(straight-forward). However here in this discussion they are trying to control volume when phone is locked via some other way - LINK
However you can launch your application when the phone is unlocked by using volume down/up event or combination using the receiver logic. You can register for "android.intent.action.MEDIA_BUTTON" to receive the broadcastreceiver.

Android- Using Power Button as trigger to start application

I am trying to build an app with the ability for it to be shown over Lock Screen when the physical Power button is pressed say 2 times in quick succession or long pressed.
So far I have figured out that I need a-
1) SERVICE- I need a Service that should intercept the Power buttons pressing 2 times / long press- whether the device is locked or not
2) RECEIVER- User Broadcast Receiver to capture the broadcast intent from service and launch my main activity.
A lot of questions on SO are similar but none of them address how to do this because-
The Service cannot have methods to detect Key Events & as a result need to find some other way to figure out when Power key is being pressed. The suggested alternate is to use SCREEN ON and SCREEN OFF intents. But using them causes a problem if the service is running in background and the Screen is woken by some other app, eg an incoming call.
I have seen few apps which use Power button to start apps or activity.
1) Press It- https://play.google.com/store/apps/details?id=com.incrediapp.press.it.macro.creator.time.saver&hl=en
2) Power Button Flashlight- https://play.google.com/store/apps/details?id=com.brink.powerbuttonflashlight&hl=en
Any ideas on how to start an app/activity over the press of Power buttons?
And then show it over the lock screen.
Short answer: You can't! The system wasn't build that way.
How ever many times when someone says: "You can't" he actually mean you can't without hacking it...
So here is an idea for a hack for you:
As you mentioned, services can't listen for key events, but Activity can. So activity is your answer.
Start your activity when the screens goes off, and kill it when it comes back. Also make it transparent just in case you get out of sync and you don't want to be spotted.
Now you got an easier problem to solve, how to trigger the power button from inside your activity.

Listen to volume up button and activate a process in android

I want to keep listening to long press of volume up button in background when triggered I would like to get some process done.
I am able to do this inside my application without any problem. I want to achieve the same when my application is not running - figured out service would be the best deal but I believe service can't listen to key events so what is the work around can I achieve the same in broadcast receiver? Can receiver run in background without the application running?
Can somebody help me out with this please?
Thanks!!
Android SDK doesn't intend for you to be able to do this while your app isn't running in the foreground. Maybe it is possible with the NDK or if you know how to touch the operating system with root access (but this would require the user to root their device).
I researched this quite a bit a while back and this was the best I could come up with, it relies on the fact that the screen turns off and on when the power button is pressed (I haven't seen any devices where this isn't the case). This event IS something you can receive when your app is not running.
Register a BroadcastReceiver or Service to receive intents with the following IntentFilter:
IntentFilter f = new IntentFilter();
f.addAction(Intent.ACTION_SCREEN_ON);
f.addAction(Intent.ACTION_SCREEN_OFF);
When the Intent is received use
System.currentTimeMillis()
to get the timestamp that this intent was received.
To make sure that the user really intend to trigger the event you have implemented, you need to chain multiple presses of the power button. Save the last time that the event was received with SharedPreferences.
Upon subsequent power button presses check that the current press did not occur too long after the previous one. If it did, restart the number of presses that have occurred (with SharedPreferences again).
If the current power button press received is within this maximum time frame, and the number of times that have been pressed reaches a certain amount (I use 4 as the default, 3 seems to cause unintentional triggers), then trigger your event.
Here is the code I have for the receiver:
https://github.com/eskimoapps/count_stuff/blob/master/Receiver.java
It's not very good and is from the first app I published a long time ago. If I ever get around to it I will rewrite it and put the whole thing on GitHub.
If you want to see it in action here is the store listing:
https://play.google.com/store/apps/details?id=count.stuff&hl=en
I would have listed someone else's app to avoid self-advertising, but I don't know any other apps that do this.

Listen for ringer muted by volume button click

i need to know in a background task if the user muted the ringer by clicking the volume button when a call is coming in. so there are 2 possible events that i could listen for:
volume button click (onKeyDown) - that would be the preferred method
ringer gets muted
it seems though that this is not possible. anyone has an idea how to accomplish that?
Thx
Simon
There are no broadcasts for exactly what you want, but you can set up a content observer to be notified when settings change (including volume). Please checkout the answer here:
Is there a broadcast action for volume changes?
Use BroadcastReceiver for this with android.media.RINGER_MODE_CHANGED
Also check AudioManager.VIBRATE_SETTING_CHANGED_ACTION ("android.media.VIBRATE_SETTING_CHANGED")
FYI: this is not possible, since the app in the foreground (the Phone app in the moment when the phone is ringing), handles the hardware button click. the event is not passed further.

How to launch android application when volume key is pressed android

i want to launch my application when the volume turned to very low..
Edit:
Or my application should run in background and it should listen to the volume changes.. I think it sounds good...
Is it possible to do that?
Use registerMediaButtonEventReceiver with a BroadcastReceiver that handles ACTION_MEDIA_BUTTON.
This thread explains it:
Volume change listener?

Categories

Resources