android background service that triggers manual events - android

I recently discovered that I could connect my wireless keyboard and mouse with my phone using an OTG adapter. My phone now has an always available, physical keyboard as well as a cursor.
I was curious if I could reroute the keyboard input to preform different manual touch events when not being used for actual typing. For example, I want various keys to trigger a manual click in the appropriate areas to reload, crouch, shoot, move, etc. in PUBG mobile.
In an active activity, I can capture a keyboard event as follows:
public class MainActivity implements KeyEvent.Callback {
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) { ... }
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) { ... }
#Override
public boolean onKeyMultiple(int keyCode, int count, KeyEvent event) { ... }
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) { ... }
}
I am unfamiliar with background operations on Android, but the developer page over viewing background services explicitly states that:
It can't interact directly with your user interface. To put its results in the UI, you have to send them to an Activity.
Work requests run sequentially. If an operation is running in an IntentService, and you send it another request, the request waits until the first operation is finished.
Both these limitations prevent a background service from being a viable option for my keyboard trick, not to mention I have no clue how to capture keyboard events in these services.
What other options do I have?
Also, would I get banned from PUBG mobile for successfully setting up such a thing?

Related

How can I use onTouchEvent() in softkeyboard?

I know that onTouchEvent()
can return the current touch point's x,y in a view,
However, when I touch a key on a softkeyboard.
the method is not working,
My question is how can I apply onTouchEvent to softkeyboard on Android?
It is not possible to get a touch event for a soft keyboard.
If you want to capture keys as they are entered into an EditText, check out the EditText.addTextChangedListener() function.
Alternatively, try implementing the View.OnKeyListener interface in your Activity, or overriding public boolean dispatchKeyEvent(KeyEvent e), or overriding public boolean onKeyDown(int keyCode, KeyEvent event). In all of those cases, you can check what the keyCode or KeyEvent is and handle it as you wish.

Override android headset hook long press

I need to override the android heaset hook button, the long press causes the music player starts auntomatically and I need to avoid this.
Is it possible in Android?
I try:
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK) {
return true;
}
return false;
}
But it not works.
Thanks,
It is possible to just create your own gesture recognition to account for a long press. You can start a timer on KeyDown and then check that timer on KeyUp to see if the key up was at or lower than your long press time. Or you can use the getEventTime methods to do the same function. If your question was more specific on how to intercept the headset buttons I would recommend this article, Allowing applications to play nice(r) with each other: Handling remote control buttons.
If you need some other kind of help or some code samples let me know

Reading input of hardware keyboard within application

I want to know if there is any provision that allows me to read the input key from a hardware keyboard present on the phone or connected externally in android . I basically am building an application that needs to have special shortcuts set . I researched about the WindowManagerPolicy but eclipse does not seem to support any interface of that sort . I need help even starting .
Thank you
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return super.onKeyDown(keyCode, event);
}
override activity's onKeyDown to catch keyboard event

Android capture hardware keyboard event without edittext view

How can I capture the hardware keyboard events without using an EditText field?
For example, in a simple activity the display a square on the screen, when a "B" is pressed on the slide keyboard I want to turn it blue, when a "G" is presses, turn it Green, etc.
I don't need help with the color code, just how to intercept the keypress
This is not about the soft or virtual keyboard
Android classes usually provide event handlers, you can implement when subclassing them. The Activity class has the following event handlers:
onKeyDown(int keyCode, KeyEvent event)
onKeyLongPress(int keyCode, KeyEvent event)
onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)
onKeyShortcut(int keyCode, KeyEvent event)
onKeyUp(int keyCode, KeyEvent event)
In addition all views have the following event handlers:
onKeyDown(int, KeyEvent)
onKeyUp(int, KeyEvent)
I guess there are many other classes that have similar event handlers for key events, but this should be enough for your situation. The KeyEvent then contains information about the pressed key, i.e. the key code.
Activity class has already implemented KeyEvent.CallBack see here
you just need to override these methods and implements all events that you want

Android ==> disable multi tasking?

1- How can i disable multi tasking?
My application is a socket based game, every time i start the app, it MUST load the main page first to start the socket connection?
I do not want the user to be able to run my application in background.
Is this possible to do?
2- I do not want the user to be able to use the back button, to navigate between pages, users must only use the buttons available in my application to navigate? is this possible?
You can catch the back button (and ignore it), but you cannot block the Home button.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
return true;
}
...
Remember, the Phone is just another application so this OS design prevents a rogue application from disabling the "phone" aspect of the device.
If you want to prevent your application from running in the background, you can close the activity from within the onPause() method:
#Override
protected void onPause()
{
super.onPause();
finish();
}
This will force your application to start from scratch if it is put into the background for any reason. This will probably be called when the phone is put to sleep, however, so it might not be the exact behavior you are looking for.

Categories

Resources