set application to run in foreground - java

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

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?

Android - How can a Service App dispatch KeyEvents to another Foreground App

I want to develop an Android Service app that dispatches KeyEvents to the foreground application.
Somehow, it would be like this
I cant seem to find a way how, I am only familiar with the activity dispatches KeyEvents to itself, like this:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (event.getKeyCode() == KEYCODE_1) {
// Do something
} else if (event.getKeyCode() == KEYCODE_2) {
// Do something
}
} else if (event.getAction() == KeyEvent.ACTION_UP) {
if (event.getKeyCode() == KEYCODE_1) {
// Do something
return false;
} else if (event.getKeyCode() == KEYCODE_2) {
// Do something
return true;
}
}
return super.dispatchKeyEvent(event);
}
but not with another App.
Correct me if I am wrong, according to what I have researched so far, it says that I need to install my application as a System App and send my keyevents directly to the android system, and the android system will be the one who will send it to the FOREGROUND App. Where it would look more like this:
If this is correct, could anyone help me how to do this? Or if otherwise, please give me a good workaround to achieve my goal. Thanks!
PS: It will be installed on a rooted device
You can definitely do this with root only and without installing your application as a system app.
The way this can be accomplished is by using uiautomator framework. This is such a powerful framework available for android with the purpose of black box testing applications.
Obviously you are going to use this framework for a slightly different purpose than black box testing, but that's ok.
UiDevice has the method pressKeyCode(int keyCode) that can be used like this to send input to whatever app happends to be in the forground:
UiDevice dev = UiDevice.getInstance();
dev.pressKeyCode(KeyEvent.KEYCODE_1);
The setup to use uiautomator can be a little complex, see this other answer of mine where I describe in more detail what you need to do.

Android Global KeyListener necessary?

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.

How to not save to SharedPreferences if cancel was clicked on a ListPreference

I need to make it so that if cancel or back was pushed when a ListPreference was open that, the value is not saved in SharedPreferences and takes no effect on the app. I have everything in my application working and have been searching for some type of solution but could not find it.
Thanks!
Edit:
Is there a way to start the list preference with none of the values selected?
Alright I was playing around and figured it out. The way I did it was I made a string in the res/values/string.xml and gave it its own id(#+id/none). Then in the res/xml/preferences.xml in the ListPreference info I added android:defaultValue="id/none". Now when I open the list of preferences nothing is selected as default.
You can handle cancel and back press event explicitly in your app and do the stuff accordingly.
Back button can be handled in following way:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}

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()
{}

Categories