Android Global KeyListener necessary? - java

I want my app to start, after the back button was pressed 3Times.
So I created this class which is executed in the Launcher.
public class KeyManager extends Activity {
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(KeyEvent == "Back key"){
Log.e("KM", "PRESSED!!!!!"+keyCode);
}
//return super.onKeyDown(keyCode, event);
return false;
}
}
This code works, if it s implemented in the launcher class, but it does not work, if the app is in the background. How or which key listener do I need for listening to background keys or more if the app is working only in the background?

This is not possible due security reasons. It's more likely a keylogger. If you really want to do that use an AccessibilityService which let you capture the KeyHooks even on a non-rooted Device. Warning: This is Dangerous and user must explizit enable it, to get it activated.
You may/can also modify the default keyboard, to capture those.

I want my app to start, after the back button was pressed 3Times.
You are welcome to download the Android source code, alter the operating system to contain this feature, compile it into a ROM mod, and install that ROM mod on your device.
Otherwise, what you want is not supported by Android.

Related

Android studio-java focus on listitem doesn't work

I'm trying to reverse engieering a Dialer from a 4.4.4 android device.
I added a onkeydown listener ,so when the user press the '9' button,the listview in the callLogActivity should scroll to top.
Of course I'm editing the smali files,not the java itself,but I'll attach a java code of everything I tried,which didn't give the desired result
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == 16) {
listview.requestFocus();
listview.setSelection(0);
listview.setSelectionAfterHeaderView();
listview.setItemChecked(0, true);
return true;
return super.dispatchKeyEvent(event);
}
}
As you can see,I tried many similar methods, such as setSelection() and setItemChecked() and more....
Actually,The list indeed scrolls to top,but the focus(selected) still stay on the current ListItem..
It's not just a visual effect problem,the focus really stay on the current item(you can see it when you just hit the "ok" button).
If you were wondering what's the meaning of "pressing the ok or '9' button" ,it's the time to expose that device model, it's the xiaomi qin 1s,a feature phone with android OS on it.
Did i miss something?

App won't close (Glass, Unity3d)

I have an Android Studio project, generated from a Unity3d Project.
I wanted to run my project on a Google Glass so I followed this tutorial (http://forum.unity3d.com/threads/how-to-build-for-google-glass.219558/).
Everything works fine so far, but I can't close the app via swipe down, even though I added this to the UnityPlayerActivity.java:
#Override public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){ finish(); }
return true;
}
Again, everything else works just fine.
1st UPDATE:
I tried the application on a tablet - like on the Glass, "Back"-button does not work.
Then I implemented this in Unity:
if(Input.GetKeyDown(KeyCode.Escape)){
Application.Quit();
}
Now it works on the tablet, but the Glass-situation did not change.
2nd UPDATE:
This also didn't work: this.finishAffinity();
3rd UPDATE:
I tried to close my Unity Application using this Coroutine in the Start()-Method:
IEnumerator waitAndExit(int sec)
{
yield return new WaitForSeconds(sec);
Application.Quit();
}
This successfully shuts down my application, so the problem appears to be this:
Unity does not recognize swiping down on Google Glass as a keyEvent in Input and the UnityPlayer object seems to get ALL input from android.
How can I stop that?I wan't to handle touchpad events with java, but my original idea - overriding onKeyDown - did not work.
4th Update:
I tried to recognize a gesture like in this answer and added this to my onCreate():
mUnityPlayer.setFocusable(false);
//mUnityPlayer.requestFocus();
Unfortunately it did not help.
This is not really a solution, but rather a work-around:
Using contextual voice commands works fine, even with the UnityPlayer-object blocking touchpad input.
I now close my app via "OK glass - quit".
If anybody figures out how to solve it properly - I'll test, verify and accept your answer.

Android Application for Kids

I'm Developing Android Application for Parental Controlling, so I want to disable home and back button. Because kids are unpredictable, their can push home button or back button and so on. Than i want to disable sliding notification bar. (Because notification bar can change device configuration and setting)
I'm planning to create Android Application which is similar to the Samsung Kids Mode-Parental Control.
Is possible to create application like Samsung Kids Mode? Can you give some link/article or even example program to me?
I Have tried this :
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
but it not handle home button, the Home button still working.
Thanks in advance to everyone for taking time to read this, hoping a positive solution.
I dont know of anyway to override the home button. Im not sure that it can be done. You can override the back button like so.
#Override
public void onBackPressed() {
//do nothing, important dont call super.onBackPressed
}
Another this you can do is set the app to immersive mode to reduce the chance of accidently exiting the app
https://developer.android.com/training/system-ui/immersive.html
Extended from Activity
#Override
public void onBackPressed() {
}
can be used to override back button. Be sure to remove super.onBackPressed().

Android Back Key Issue

I'm developing a game via Andengine for Android. I have big problem. When I open my game first time, it's opening well. And when I press back key button(which is on device, pyhsical button), the app is closing. But it mustn't be close. And here is the LogCat:
http://s27.postimg.org/3mhwb3j2b/Capture.png
And when I try to open app again, it is opening black blank scene.
PLS help!
try to override onKeyDown and block BACK button
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode==KeyEvent.KEYCODE_BACK)
return true;
return super.onKeyDown(keyCode, event);
}
That should prevent user from exit App with Back button
You should override the onBackPressed function, and do what ever implementation you like. Usually a pause of the game is common with a dialog to quit or continue is used.
#Override
public void onBackPressed()
{}

set application to run in foreground

Is there any way to set my application to run in foreground while pressing any key, even the home button? I have tried with avtivitymanager, but I didn't get o/p. I think you may be able to help me.
Ashwin Kanjariya
you can stop functioning of all buttons,,,,
You just override the home and back button of device
public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
if (keyCode == KeyEvent.KEYCODE_Home) {
//finish();
}
return super.onKeyDown(keyCode, keyEvent);
}
In your activity class your application will not be interrupted as the buttton default functionality is overrided as you desire...
I find out that you can catch keypresses by overriding one of the onKey..(int keyCode, KeyEvent event) functions in the activity, but you can not override the home key for some reasons.
home key in adroid API
onkey events
question about button overriding
question about home overriding

Categories