how to response click event when screen off - android

i have a app,if my app screen off some times,i want to click the screen to awake the screen on.i know i can press the power to deal with using flag_user_present to receive the broadcast or
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
//do nothing but show a warning message
Toast.makeText(this, "you pressed the power button",Toast.LENGTH_SHORT).show();
return true;
}
return super.dispatchKeyEvent(event);
}
but i want to click the screen to do the same thing ,so i overload the ontouch event,but when screen off,the ontouch not receive focus and deal with the click event.
so my question how to deal with click event when screen off(my screen not lock and stay the same activity,only screen off)

You won't be able to do this when the device shuts off the screen. If the screen is off, then the touchscreen usually get's shut down immediately by the kernel (if it's not modified otherwise). This happens for obvious reasons.
You could use what tsp said in his answer, and emulate the screen off by turning it black, turn the brightness as far low as possible and then still listen for touch events. Keep in mind though, that this behavior is unexpected by the user and he might think the screen is off, where it isn't. I don't recommend doing this! Also, not all devices allow a zero brightness.

u can use
android:keepScreenOn="true"

Related

Detect/override controller "Home" button press android

I have an android application that is meant to be used with a game controller connected to the android device.
Most game controllers have a center 'home' button that I would like to use to pause the game (for example, an Xbox controller center nexus button should pause).
However, when you press the center nexus button on the controller it causes the entire app to close as though the actual home button on the phone was pressed. Why is the center button on some controllers causing the app to return to the home screen? Note, this doesn't happen with all controllers or all devices. For example, using a pixel 6 and an xbox controller it doesn't send the app home. But using a galaxy a32 the center nexus button does triggers the home action.
I understand its not possible to intercept the actual home action from within an app. But is it possible to do so from a physical controller? Or is it possible to remap the controller buttons?
I tried this in an attempt to intercept the home action:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.e("KeyPress", "Code: " + keyCode);
switch (event.getKeyCode()){
case KeyEvent.KEYCODE_HOME:
return false;
default:
return super.onKeyDown(keyCode, event);
}
}
but this isn't working. Pressing the center button on the controller doesn't trigger the log but it still goes to the home screen.
Any ideas how to make certain button on physical controllers not trigger the home action?
Thanks
This happens with gamepads that send the same input code used for the "Homepage" key on some keyboards. By default, HID input devices are handled by the hid-input driver which always translates the "Consumer Application Control Home" usage (000C:0223) to the KEY_HOMEPAGE Linux input event code.
https://github.com/torvalds/linux/blob/4e23eeebb2e57f5a28b36221aa776b5a1122dde5/drivers/hid/hid-input.c#L1151
The Android OS converts KEY_HOMEPAGE to KeyEvent.KEYCODE_HOME which is handled by the framework and never delivered to applications. Probably it should convert it to BTN_MODE as suggested by the Linux gamepad specification.
If you have root access you can fix this by adding a Key Layout file to correct the button mapping for a specific device. See Vendor_045e_Product_02fd.kl for an example of a key layout file that maps KEY_HOMEPAGE (Linux input event code 172) to Android's KeyEvent.KEYCODE_BUTTON_MODE.

Immersive mode instructions reappear every time the device enters this mode

My app uses the new "immersive mode" by calling (in onCreate):
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
This works great, but the "how-to" popup ("Swipe down from the top to exit full screen") appears every time the Activity is launched (if the phone is being locked while the activity was showing), even though the user has acknowledged the popup. As far as I understand, the popup is automatically generated by the system, so there's nothing I can do to change this situation, correct?
This issue is reproducible as follows:
Launch immersive Activity [no popup appears, only on the very first launch (correctly)]
Press the power button to switch off screen while the activity is showing
Press power button again to switch on screen
Close Activity by calling finish() e.g. from a button or menu option
Launch Activity again - popup reappears
The popup does NOT reappear if the activity is launched, closed, and relaunched without hitting the power-button in between. Also, it ONLY reappears if the activity was topmost while the power button was pressed.
Correction: The Activity needs to be closed by calling "finish()" (e.g. from a button or a menu option). It works correctly if the Activity is closed by the back-key.
I've uploaded a sample app here: https://github.com/niko001/com.greatbytes.immersivebug/tree/master/Test5
EDIT: There's now an Xposed module to disable the "panic mode", so I guess I'm not alone in seeing this is an annoyance ;)
Really interesting question! Thanks to your clear instructions, reproducing the issue wasn't a problem.
Alright, after digging through the source for almost 30-minutes and saying why would they do this? a bunch of times, I think I finally get it. I'll try to explain the best I can, but this is only my interpretation, and may not be correct:
Someone at android realized that the Immersive Mode will send people into a state of panic: how do i exit? (_sorry, I don't know what else the panic would be about_).
In this state of panic, the user will turn to the POWER BUTTON
.... > Power button --> User turns screen off (at x milliseconds since EPOCH)
.... > Praying that the navigation bar comes back
.... > Power button --> User turns screen on (at y milliseconds since EPOCH)
Now, the duration y - x is of significance. We'll discuss it a bit later, but first, let's look at how panic is defined:
panic happens when Praying the navigation bar comes back lasts less than 5 seconds. This value is held by:
mPanicThresholdMs = context.getResources()
.getInteger(R.integer.config_immersive_mode_confirmation_panic);
<!-- Threshold (in ms) under which a screen off / screen on will be considered
a reset of the immersive mode confirmation prompt.-->
<integer name="config_immersive_mode_confirmation_panic">5000</integer>
Ah, okay. So, it doesn't matter if the user has already acknowledged once, the prompt will be back if the above-mentioned criterion is met - even on the 100th launch.
And here's where the action happens:
public void onPowerKeyDown(boolean isScreenOn, long time, boolean inImmersiveMode) {
if (mPanicPackage != null && !isScreenOn && (time - mPanicTime < mPanicThresholdMs)) {
// turning the screen back on within the panic threshold
unconfirmPackage(mPanicPackage);
}
if (isScreenOn && inImmersiveMode) {
// turning the screen off, remember if we were in immersive mode
mPanicTime = time;
mPanicPackage = mLastPackage;
} else {
mPanicTime = 0;
mPanicPackage = null;
}
}
(time - mPanicTime < mPanicThresholdMs) ==> ( y - x ) < 5000
unconfirmPackage(mPanicPackage) removes mPanicPackage (yours) from the list of packages stored under Settings.Secure.IMMERSIVE_MODE_CONFIRMATIONS.
Needless to say, I find this strange... and wrong. Even if the user is in panic, and takes the power button route, s/he won't see the helpful reminder until next launch. So, what's the point?
Or may be, I am wrong about the definition of panic.
so there's nothing I can do to change this situation, correct?
Correct. To fix this, you would have to add your package-name to value held by Settings.Secure.IMMERSIVE_MODE_CONFIRMATIONS. But, to write to a secure setting, your app requires WRITE_SECURE_SETTINGS permission - not for use by third-party applications.
Links:
ImmersiveModeConfirmation (helper class that manages showing/hiding of confirmation prompt)
More succinctly - in K, users will see the confirmation when entering immersive mode if:
They have not yet confirmed it for that app (package).
They "panicked" last time they were in immersive mode. "Panic" in this case means toggling the screen off, then back on in under 5 seconds (by default).

Stop screen locking on power button press on **rooted** android

So when Power button is pressed one time then the screen is off or basically locked. I would like to alter this behavior so that if power button is pressed then the screen should not go off. As per discussion here it is not possible Stop the Screen Locking when power button is pressed
but then what I have is a rooted one. Is there a solution where I can use su privileges to achieve this somehow?
So far I tried this code snippet but this is not invoked when power button is clicked only once. If I do a long press on Power button then I can get to this but my objective is to get to it for just one click.
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
//dostuff
return true;
}
return super.dispatchKeyEvent(event);
}
Any Suggestions?
I'm afraid simply rooting the device won't help you. Unless you can get your hands on firmware that's suitable for your device and allows this behavior this will not be possible to implement.

How to prevent user from closing application - Android 4.0.3

So my question is: If is possible to prevent user from closing application.
Problem is because i can't hide action bar and i use tablet only for work time registration. So if someone press home button or back button is that not acceptable.
So i wonder if i could somehow handle onclose event?
Is it possible to open application in fullscreen (with no action bar)?
You cannot prevent user from closing application if he presses home button. That's the whole idea of it. Otherwise, you could leave user trapped in your app with no means to exit but to reboot it's device.
Short answer is no.
Long answer is that you can make it quite difficult for the user to close you app. Some of the tricks that can be used are: reopen you app as soon as it closes , disable keys like back and power and finally disable the home button
A user will always be able to close an application, otherwise there would be programs abusing it and causing problems, but there are steps you can make to better handle it being closed.
For example, if you have a remote service running that can check if the application is running, and there can be various ways to know, then it could fire off an intent to start the application again.
One way to know if a program is alive is to have it periodically call the service, basically doing a heartbeat check, and if it hasn't been called in some period of time, which should be 2 or 3 times larger than the expected check-in period, then fire off the intent.
There are other steps that may work, if you detect that the home button was pressed, but I would need to think through those steps. I think it depends on your expectations though, as trapping someone in your program would be really bad.
try this...
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Ref-> How to hide the title bar for an Activity in XML with existing custom theme
to prevent the user .......
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK || keyCode==KeyEvent.KEYCODE_HOME){
// pass some msg ......
return true;
}
return super.onKeyDown(keyCode, event);
}
So here is my solution that works...
First you install trial version of SureLock application. Then in that app disable action bar. And thats it.
If you want to view again action bar you will need HideBar app
All of that works only on rooted devices...

Using volume rocker when screen is locked

I'm a beginner at programming in general. I don't even know how to begin to search for something like this. What i want to do is make a simple text app that counts. Mainly i want to count how many fish i catch. I want to be able to use the volume rocker when the screen is locked to count how many fish i catch. I haven't started on the app yet, so i don't have any coding to post. If someone can point me in the right direction, that would be wonderful. Thanks in advanced.
This code will get started listening for the volume button presses. I imagine if you want to do it while the screen is off you'll have to acquire some kind of wakelock that lets the screen shut off but still keeps your app running.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){
//Do something
}
return true;
}

Categories

Resources