Is it possible for a service to simulate power button? - android

I'm writing a service on android and I need to turn off the screen after a certain time, I tried using all solutions on this thread enter link description here
but none of them worked, thus is there any way i can simulate the power button?
Thank you

Related

How to detect the power button press in a System app in android

I have a custom ROM of Android and want to launch my app if power button is press specific number of times. I will make my app a System app so that it will have system privilege. I know power button press broadcast is not possible for user apps but for system apps it should be possible, if anyone knows please help me.
For example: Three times press the power button call the SOS number.
Simplest way would probably be to put your code in frameworks/base/services/core/java/com/android/server/GestureLauncherService.java.
Take a look at the patch implementing double-tap to launch camera for reference. https://android.googlesource.com/platform/frameworks/base/+/5941c98%5E%21/

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.

How to prevent powering off an android device by Power button?

I'm developing a security program that prevent moving phone without knowing its owner. When program is running when phone moves alarm will fire and show unlock screen to spot the alarm. The alarm can be stop by a password. But the problem is when use press and hold the power button, phone will shutdown. So is there any way to disable power button action?
So is there any way to disable power button action?
There is no way for an ordinary Android application to prevent the power button from functioning, for blindingly obvious security reasons.
It is conceivable that there are ways to accomplish this on rooted devices. Also, if you create your own custom ROM, you can integrate your alarm feature into it, blocking the power button operation while in the alarm state.
There is no way to work around this, because even if it was possible to prevent a shutdown, there would still be the option of simply removing the battery.

Turning off Bluetooth when exiting an application

As an introduction to my question, I'll tell you that I've searched around the correct way to do things but still didn't find an answer.
I saw in many questions asked in stackoverflow, that exiting applications in Android is done simply by clicking the 'home' button. I saw some people say that it is not a common use to have an 'exit' button on your app.
Here comes my question. Suppose I turn on the Bluetooth coonection in my app, so that two players can play one against each other with two phones/tablets. Now, they want to go out or they want to go on in their lives. How can I turn the Bluetooth off? is there a timeout for application that does this for me? Should I start a downtime counter to turn the Bluetooth off? What if the user decided to go out to read an SMS and then come back to the app, discovering the bluetooth is off?
In other words, what is the correct manner to deal with shuting down Bluetooth when it is not needed any more with out interfering the user?
Sorry for the long question, I just want to make myself clear.
Thanks,
AJ
You can override the onPause() method in your activity to disable bluetooth, and override the onResume() method in your activity to enable it again.
However, I would not recommend this. For starters, it takes a good second of time to switch the hardware on/off. Also, what if the user is using some other bluetooth connected device? Pausing your app would cut that connection off. What if bluetooth was already on when the user starts the app? They probably wouldn't want your app changing that.
My recommendation is when you start your app, look and see if BT is enabled. If it is not, prompt the user to turn it on. If you're really set on cleaning up after yourself, you could add a checkbox to the prompt I just mentioned, that says something like "Disable it again on exit", and let them choose if they want that.

Check volume button usage when screen is off

For this question I'm going to quote another user who got no response to their question:
I've written an Andoid app that uses the hardware Volume buttons for another purpose.
It works fine if the app is running and visible, but when I turn the
screen off or let it time out, the button clicks don't get into my
handlers.
Does anyone know if there is a way to detect these button clicks when
the screen is off?
Source: AV695's question
I'm working on an app myself that makes use of the volume buttons, but as this user also noted, the normal behavior of checking buttons with onKeyPress stops working once the screen is off. This is because the Activity gets paused on screen off.
Is there a way to keep the activity running while the screen is off, or check for the usage of the volume buttons when the screen is off? I tried using a Service for this before but it's impossible to check for the volume keys like that as noted by Commonsware.
I doubt that this is supported (without resorting to a battery-draining wakelock) at either the platform, kernel, or underlying radio firmware levels without modifications to the last to bring volume presses during sleep to the attention of the kernel.
Within the realm of reasonable system-ROM modifications, a more reasonable one might be to modify an existing open source ROM for the device to insert some custom platform level code into the handling of the power button usually used to wake up the device preparatory to unlocking it - that at least we know does get the attention of the kernel. That code could then inform the user by sound or vibration if there are unacknowledged notifications.
You could optionally wait briefly, check device orientation, or look for another key press to avoid doing this in an annoying way when the user is holding the device outside their pocket and trying to unlock it.
Or you could not use the volume key and just set a timer to wake up every 15 minutes and vibrate if there are unacknowledged notifications, avoiding the need to fumble in ones pockets.
You mention it's a custom request: if implies it's one off or low-volume, another option to consider would be that a few vendors have "bluetooth watches" out with an SDK that lets you push notifications from an android device.
If you can capture the notification when it's generated, you could push it to the user's wrist, and then let the phone go back to sleep.
You cannot intercept the key while your application is in background, but instead of listening to the KeyPress itself. You can register a ContentObserver, as described in this question.
As Chris Stratton mentioned, the only way to keep your App alive is by using battery-draining wake locks.
However, since I found myself in the same situation, I came up with another solution. Unfortunately, you'll need a rooted device as well as the Xposed framework.
With Xposed, which replaces the zygot process so you can hook yourself into any constructor and method of the system, you will be able to catch the raw KeyEvents before the system handles them.
This is done in PhoneWindowManager.interceptKeyBeforeQueueing(). By using a XC_MethodHook, you can use beforeHookedMethod() on the afore mentioned method to catch every hardware button event, even if the device is in deep sleep.
After catching events you are interested in, you can create a temporary wake lock to do your things but don't forget to release the wake lock after you finished your work.
A good example of how to accomplish this is the Xposed Torch Module.
If you, however, rely on a non rooted system, the bad news is that it's simply not possible without draining the battery...
I was also trying to implement volume button press detection in my app and I left that part to be developed later once the core part is done. I was able to detect volume key press while screen is on even when phone is locked, from a background service.
Background Video Recorder 2 (BVR2) (and possible BVR1 also, I did not try) is one of the apps that can detect volume key press even when screen is off. While trying to implement volume key detection while screen is off in my app, I installed BVR2, hoping to find how it works. To my surprise it gave my app the ablity to detect volume keys even when screen is off. My app had a ContentObserver to monitor volume changes, but was not working when screen is off. When BVR2 is active my app also could detect volume key press when screen is off. Still digging.
But BVR2 has its own trigger action, that is to record video, an action you may not want to occur just for the sake of you application detecting volume key presses.
Another app is QuickClick. This app can give your app what it lacks, the power to detect volume key presses even when screen is off, without extra unwanted actions. Just install QuickClick and do not configure any action. Create a ContentObserver to monitor for stream volume changes and you are ready. You app will now be able to detect volume key presses even when screen is off.
Please note that my app runs as a background service.
Both of the apps mentioned above are meant for other uses, but uses volume key detection to perform action. I am in no way connected to any of the apps mentioned.
If these apps, and possibly dozens others, can detect volume key press, it can be done. I request experts to find out how to do it, so that we can implement in our app without relying on another app.
If you find this answer useful, please up-vote.
I am not sure if it is as simple as this but check this android blog:
Allowing applications to play nice(r) with each other: Handling remote control buttons
It explains the usage of a broadcast receiver that receives the up/down volume controls and other music controls.
In summary you should use registerMediaButtonEventReceiver

Categories

Resources