I have an activity that shows up when the phone screen goes to sleep/turns off ie turns black.
For some reason, the phone turns on when the volume buttons or the camera buttons are pressed. By turns on, I mean the screen wakes up or comes back from the black screen state.
I've tried using dispatchKeyEvent(KeyEvent event) and the buttons are disabled on the activity, but they still wake up the phone.
You could try overriding the onKeyDown(KeyEvent) method and change what happens for those keys. However, I'm not too optimistic as if you're running an activity, it will be in an inactive state when the display is off, and also it could be that the phone is hard wired to wake up on those buttons. It could be device specific. Hard to say. Try that out and let me know how it goes, I'm currious
Related
I'm developing a scientific app in Android Studio. It works smoothy.
The set of source code files is not small, but, as I don't have practically user interface, there is only one activity and there is no intent.
All initialization code is inside OnCreate. Most of times, my app preserves all data, when he gets out of the foreground.
However, maybe (I cannot find a pattern of this event) he loses all data and restart (shows a white screen for 2 / 3 seconds), even if the cell phone don't enter in lock screen and there are just 2 apps running.
There are situations that I comute for another app (like WhatsApp) and resumes for my app, and my data was gone. The app restart again.
There is no error message, no logcat. Nothing.
Mostly, when I lock the screen and enter again, all my app data is there.
PS: My orientation is locked.
PS 2: I've read all related question and there is no hint for me. Based in one answer, I've tried to put in onCreate the following code.
if (!isTaskRoot() {
&& getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
&& getIntent().getAction() != null
&& getIntent().getAction().equals(Intent.ACTION_MAIN)) {
finish();
return;
}
No changes for me.
Update:
I've stumbled in solution. it can be read in my own answer. it's related to undesired back button effect for one-activity-app (read here and here ).
For me, as my application has only one activity, back needs to be like a home button: exit the app but preserve all activity data. My app has a real exit button, where the user shows that really wants to do this.
It's my first app that I developing in mobile world and, for extension, Android world
Some problems seems to me like that it is only possible find the solution if one has a hint about its solution. it's a contradiction. One doesn't know but has to know to solve that don't know!
And, in this situation, it's not the case. No hints. Just question marks.
Before, I had not noticed any pattern. People sometimes act so automatically ... However, suddenly the penny dropped.
I've stumbled in solution. Fortunately!
Not in a million years could I suppose that if someone has an activity and presses Back button, (right button in the bottom), you practically quit the application, even if it remains as a running app for the left button in the bottom (app switcher button)
When I've noticed it, I start to research with another focus. And I've discovered two sources: Disable back button in android and Android - Simulate Home click
So the solution is simply to make the Back button act like the Home button (middle button in the bottom). Return to the home screen without losing application data.
And this is done simply by putting in the onCreate, for my only activity, the following code.
override fun onBackPressed() {
val i = Intent(Intent.ACTION_MAIN)
i.addCategory(Intent.CATEGORY_HOME)
startActivity(i)
}
I want to detect long presses in volume key in a service. Here are my options:
A) Let the user control volume from the lock screen
I wan't to detect if the user has held down the volume button in a service while the screen is off. I already tried (for 2 days) doing it with contentObserver, but the problem is that contentObserver detects volume changes, and volume doesn't change when the screen is off. Is there any way I can let the user control volume from the lock screen?
B) Detect long press for volume button from service
How can I do this? Are there any broadcast receiver's that I can use while the screen is off? Is there a way to implement the dispatchKeyEvent in a service?
I have seen this, but for me the second answer doesn't work in the background. I think the easiest way would be option A because I already have everything set up for when the user changes volume, so can I let the user control volume from lock screen? If not, is there anything else I can do?
Thanks so much,
Ruchir
I'm new to posting, but have been using the site for help for a while now. So thanks for that!
I'm working on a new app, got everything running well. It's a kids' soundboard. 2 pages of imagebuttons in a relative layout, with onclick listeners which triggers SoundPool.
My question is this:
in testing I've discovered if you're finger is anywhere on the screen where a button is not (say pressing on the background) and then you try to touch a button (multitouch) ... the button press doesn't register. Any way to fix this?
Thanks!
You have to cover additional event types for multi-touch, if you are not covering below Actions, you are not covering multi-touch
MotionEvent.ACTION_POINTER_DOWN: This event happens for any additional finger that
touches the screen after the first finger touches
MotionEvent.ACTION_POINTER_UP:This gets
fired when a finger is lifted up from the screen and more than one finger is touching
the screen.
The explanation is quite tedious. I suggest you to google multi-touch for android. One helpful link is: http://android-developers.blogspot.in/2010/06/making-sense-of-multitouch.html
I'm working on a game and use a WakeLock to keep the screen from going to sleep while the user is playing. However, I'm encountering some weird behavior when the screen is manually put to sleep..
My current approach is to store away the game info in OnPause() and load them back up when OnCreate() is called. This works fine if I hit the home button and start my app back up. However, turning the screen off seems to call OnPause() a couple times..in addition to some other funky behavior. Can anyone offer an explanation of why turning the screen off is different than hitting home?
According to Logcat, here's the sequence of events.
When Screen is turned off-OnPause is called; OnCreate is called; OnResume is called; OnPause is called again
When screen is turned back on- OnResume is called (before phone is even unlocked)
I'm trying to make an application where the user can override the default behaviour of the volume up/down buttons (as well as the screen on/off button - is this possible?). Anyways, using some code along the lines of the following I can do this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyUp(keyCode, event);
if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP)) {
//this is where I can do my stuff
return true; //because I handled the event
}
return false; //otherwise the system can handle it
}
But I would like it to be possible even when the application is not open, hence why I'd like to set up a broadcast receiver or maybe stick something in a service to make this possible.
Thanks for any help.
as well as the screen on/off button - is this possible?
Fortunately, no.
But I would like it to be possible even when the application is not open, hence why I'd like to set up a broadcast receiver or maybe stick something in a service to make this possible.
This is not possible for the volume buttons.
For example, AndroSS allows you to override the camera hardware button to take a screenshot.
That is the camera button. If the foreground activity does not consume a camera button click, that gets turned into a broadcast Intent, which other applications can listen for. This behavior is unique to the camera and media buttons and is not used with any other hardware buttons.
this isn't a very good idea. this means every time you press the volume up/down keys you're going to capture it regardless of where you are. what if i want to change my ringer volume? media volume? app that uses the rockers for other uses? it just won't work well and just aggravate people. plus, i'm not sure if its doable.
This is not (easily) possible, because the "onKeyDown" method is overriding Activity.onKeyDown(). For this reason, you have to have a foreground activity to receive these function calls.
I don't have any experience doing this, but you would have to dig a bit deeper into writing your own hardware key intercept/handler functions, since you wouldn't be able to access the one from the activity class.
This probably won't do what you want, but if you make an IME, the IME can capture the volume +/- buttons, and do whatever it wants, like remap to other button presses. For instance, I have modified a Gingerbread-style IME to turn volume +/- buttons to do page up/down in the Kindle and Overdrive apps.