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;
}
Related
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.
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"
In Android application is it possible that when I press my mobile *(star) button(not widget button) then I can perform any particular events in my application? If it's possible, then how may I achieve it?
If you mean the * key from your hardware keyboard ( on the devices that have it) you can capture it using KeyCode.
Here you can find an extensive list of all the keys you can intercept.
To do it:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_STAR: //here you check any key you want
{
//your code here
return true;
}
}
return super.onKeyDown(keyCode, event);
}
EDIT
Answering your comment, I don't believe this is possible. The KeyDown/Up events are handled on Activities. And you won't have an Activity active. Check this out!
EDIT
Yeah, according to this guy you can't.
If the button is within your own app, then yes.
If you mean a button in any other app (I think you mean the * key on the dial pad), then no.
I am building a custom camera app and have got the basics to work. I have also been able to block the camera button from initializing the real camera app. The only thing that I would like to do is build in autofocus when the camera button is half pressed.
I am comfortable using camera.autofocus, but cannot find a way to listen for the camera button to be halfway pressed (like the default camera app does) to start the autofocus call.
Is there a keycode or another way to listen for the camera button being depressed to its half way point?
I got a little creative and just toasted any key down event in android. I ended up finding out that the key code for camera focus is 80 this way. This also matches up with the android documentation once I knew what I was looking for.
http://developer.android.com/reference/android/view/KeyEvent.html
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_DOWN){
Toast.makeText(this, new Integer(keyCode).toString(), Toast.LENGTH_LONG).show();
return true;
}
return false;
}
Hope this helps others.
The camera button is a virtual button on the screen, I am not sure how it can the half pressed, or even could be mimicked in any way.
I am trying to port my Android app to work well on tablets with Honeycomb. One issue I am having is when the keyboard is showing and the back button turns into a down arrow, I can't seem to pick it up as not only a back button action, but as a keyevent in general. Here is my code:
#Override public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(TAG, "keycode: " + event.getKeyCode());
}
So when the arrow is pointing down, I don't get a LogCat read out, and it appears it doesn't even know the down arrow is a KeyEvent. Has anyone else run into this? How can I fix it?
Look at the View.onKeyPreIme() method.
Although reading your question and your reply to Mark, I'm unsure as to whether you're writing an app or an input method.