Android- Using Power Button as trigger to start application - android

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.

Related

android replace unlock screen

I am searching for a way to implement a custom unlocking screen for Android. There seem to be lots of questions about that but no real way to do that, am I right?
Solutions I found are about creating an activity that is invoked by some mechanism, that have to disable or bypass the home key and other keys and so on.
The simplest way in my imagination should be giving android an activity and say hey if you lock the screen and the user wants to wake it up, call this and wait for it to respond if unlocking is ok or not.
Handling stays within the system, that activity does not have to receive any key events, the system does not respond to home key in this situation...
Is there a chance?

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.

It is possible to make a HoneyComb activity unclosable?

I'm developing a presentation style application for HoneyComb Tablets. At one stage the tablet may be passed around a room for people to interact with. If possible I would like to prevent malicious users from navigating away from the current activity.
So far I have overwritten the onBackPressed() to prevent finishing the activity but users can still press the other buttons on the status bar and also leave the app via notifications that pop up.
Any suggestions or possible solutions?
Thanks
1. Make your activity full screen.
2 Use an alarmManager to trigger your activity from a service on a regular interval say 2or3 second (only if your activity is not foreground). For this use a boolean variable and store it using sharedPreference. this value will be true in onReume and false in onPause or in onStop or in onDestroy. And then only start your activity from your service if the boolean variable is false. Now if your user will press the Home button then AlaramManager kick start your activiy again.
3 Make a special button for finishing your service and activity and for cancel the alarmManager.
As far as I know there is no way to capture the home button press, so this is not possible. Not to mention, it would be a bad ui design decision by the dev team. The home button is there so every Android user has a standard way to exit apps. There would be some extremely malicious apps if there was a way to make the user unable to exit an app.
I'm developing a similar application that runs in a "kiosk" fashion for retail stores. When the application launches, it programmatically hides the system bar so you cannot exit. The system bar is restored when the tablet is rebooted. It requires root/su however.

Key events in Android services

I've made some research concluded that afaik I can't have an Android service for key events capture. But - maybe some will have any workaround. I have a player application, which i want to play/pause using a special button on my earphones (included for my samsung galaxy S). This is button for reciving and ending phone calls set on phones cable.
Ive tested this button - it equals KeyCode.KEYCODE_BUTTON_B const (79). And it can be handled when my player is on screen, but I'd like to play/pause also when my phone is locked (with screen off). Do You think this is possible?
For call reciving I think android uses a trick: when someone calling, the screen goes active, and the call-reciving activity can handle key events (I think - this is how it's work, but I can be wrong).
I have no idea - how to play/pause my app using this button.
I don't think there is any real good way to achieve what you want to do from the application level. However one possible solution I can come up with is this:
Listen for screen off and on Intents. When you receive SCREEN_OFF start up a "blank" activity that does nothing but listen for your button press and passes it along to your music service.
When you recieve SCREEN_ON finish() this blank activity.
I don't know for sure that this approach will work, but I used something similar to be able to listen for volume button presses while the screen was off.
Note about listening for SCREEN_OFF and SCREEN_ON intents. When I did this I had to set the filter for my receiver in java code rather than in the manifest. For some reason when I set the intent filter from the manifest it wasn't receiving those intents properly.

Distinguish between screen timeout and power-button-press?

my app does something when the screen goes black, but I want it to only carry out that task if the screen was turned off "by itself", through a screen timeout - NOT when the user presses the power-button. Is there any way to distinguish between these two events?
ACTION_SCREEN_OFF obviously fires in both cases, and I haven't found any other intents that might match what I'm looking for.
At the lower level there's an event happening when the screen times out, take a look with "adb logcat" and you'll see it, dig from there.
Services can't directly handle button events. Perhaps you can have your Activity register a broadcast receiver to get notified when the screen is going off, and then have the Activity check the last key pressed to see if it was the power button--if it wasn't then you can send a broadcast to your service and have it run whatever you want.
That's a fun question :) Although not accurate, you can try sampling the time since the user last touch event and to the time your activity goes to pause - and see if it's fits the screen off timeout
Can you listen for a KEYCODE_POWER keyboard event? That might do what you want. (But the order in which that and ACTION_SCREEN_OFF arrive might not be guaranteed).

Categories

Resources