I have an application which has the following activities;
Login -> Home Area -> Interaction recorder (touch screen to record interaction)
Whilst this interaction recorder is active i want to be able to allow the user to exit the app through either the back key or home key and still be able to get back to that interaction recorder. However if the interaction recorder is finished (managed on a timer) then the user is taken to the login activity
Also, should I override the back key whilst in the interaction recorder because I do not wish for the user to destroy the activity during its recording
thanks in advance,
Andy
you need to disable all the keys of device and need to handle back key. Override the below method but remember you can not control the behaviour of home key and end call key ..
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(KeyEvent.KEYCODE_MENU == event.getKeyCode() || KeyEvent.KEYCODE_DPAD_LEFT==event.getKeyCode()
|| KeyEvent.KEYCODE_DPAD_DOWN==event.getKeyCode() || KeyEvent.KEYCODE_DPAD_RIGHT==event.getKeyCode()
|| KeyEvent.KEYCODE_DPAD_UP==event.getKeyCode() || KeyEvent.KEYCODE_DPAD_CENTER==event.getKeyCode())
{
return false;
}else if(KeyEvent.KEYCODE_BACK==event.getKeyCode()){
//Do your task here...
}
return true;
}
to achieve your app exit requirement while moving from one activity to another finish the previous one and start it if you need to come back ...
Related
I am using android foreground service for continuous location updates that starts when user clicks a button. My question is what if the user presses the button twice? Will there be two foreground process running? If so, how to check whether foregroundservice running already or not.
So that I can apply a condition before calling the foreground service. Please let me know if there a way.
Services are singletons- only one will exist at a time. However, it will call onStartCommand again with a new Intent if you call startService or startForegroundService again. So make sure that call won't cause you to rerequest location updates if you already have.
You can check if your service is running or not by using this function:
private fun isMyServiceRunning(serviceClass: Class<*>): Boolean {
val manager = getSystemService(ACTIVITY_SERVICE) as ActivityManager
for (service in manager.getRunningServices(Int.MAX_VALUE)) {
if (serviceClass.name == service.service.className) {
return true
}
}
return false
}
If your service is not running start the service:
if (!isMyServiceRunning(MyService::class.java) {
startService(...)
}
I am developing a keyboard app for Android in Java and I want to be able to send inputs as if my application was a virtual game controller.
Example:
The inputs I want to send are among the following default buttons (DPAD_UP, BUTTON_A, etc)
Image source: https://developer.android.com/training/game-controllers/controller-input
I see a lot of resources regarding Event listeners and how to handle gamepad or key inputs (as if I was a game receiving inputs), but I haven't found anything on the "server side", on how to actually send these inputs from one app (my keyboard) to another (the game).
I have already registered my app as a keyboard app, and I am able to send normal key inputs (such as letters) to compose a text. To to that I am overriding the function onKey() from KeyboardView.OnKeyboardActionListener.
Sources: https://developer.android.com/reference/android/inputmethodservice/KeyboardView.OnKeyboardActionListener#onKey(int,%20int[])
https://www.androidauthority.com/lets-build-custom-keyboard-android-832362/
#Override
public void onKey(int primaryCode, int[] keyCodes) {
InputConnection inputConnection = getCurrentInputConnection();
if (inputConnection != null) {
switch(primaryCode) {
case Keyboard.KEYCODE_DELETE :
CharSequence selectedText = inputConnection.getSelectedText(0);
if (TextUtils.isEmpty(selectedText)) {
inputConnection.deleteSurroundingText(1, 0);
} else {
inputConnection.commitText("", 1);
}
case Keyboard.KEYCODE_SHIFT:
caps = !caps;
keyboard.setShifted(caps);
keyboardView.invalidateAllKeys();
break;
case Keyboard.KEYCODE_DONE:
inputConnection.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
break;
default :
char code = (char) primaryCode;
if(Character.isLetter(code) && caps){
code = Character.toUpperCase(code);
}
inputConnection.commitText(String.valueOf(code), 1);
}
}
}
However, I don't see how this could help me send the gamepad Keycodes.
So my question is: what is the correct way to send the game controller keypresses from my app to another app (the game)? I want to be able to press a virtual button on my app (emulating a BUTTON_A [keycode 96] for example), and send this key input to the game.
Important: my app cannot have root privileges or be signed as a system app. So that is why I am trying the keyboard approach, so I can be registered as a virtual keyboard on the device that I want to control and then be able to send key inputs from inside my app to another app running on the same device (the game that supports a game controller).
Thank you in advance.
I know for an activity you can override onStart on onStop methods to know when an activity starts / exits. The issue I'm running into is I want to keep a session open from when a user opens the app until it enters the background / user exits and tying into onStop (for end session) for each activity isn't giving the the results I want, it ends the session every new activity. So I was wondering what are my options for knowing when a user puts the app in the background or exits.
One thing I thought of is keeping track of onStart and onStop (or any two combinations in the activity life cycle) so I know if I have a onStop without a onStart right before the app is exiting. This seems very hacky, not sure if its the right place to start.
Thanks for any input.
You can extend Application class and use registerActivityLifecycleCallbacks to build the logic.
Using for example these two callbacks:
#Override
public void onActivityResumed(Activity activity) {
mBackground = false;
}
#Override
public void onActivityPaused(Activity activity) {
mBackground = true;
}
https://developer.android.com/reference/android/app/Application.html#registerActivityLifecycleCallbacks(android.app.Application.ActivityLifecycleCallbacks)
I have a button for google sign in.
if (id == R.id.sign_in_button) {
// start the asynchronous sign in flow
mSignInClicked = true;
mGoogleApiClient.connect();
boolean mAutoStartSignInFlow = true;
builder1.dismiss();
}
but everytime, a new user wants to log in, he has to press it twice: the first time, he can choose which account he wants to use, but it's not showing the Welcome player(with the level of the player and so on). the second time it actually logs him in, with that toast-thing from google on the top of the screen 'logged in Welcome player(with the level of the player and so on)'
how can I do that he only have to press once?
the first time, he can choose which account he wants to use. the second time it actually logs him in,
If user has multiple accounts on device, Account Manager will ask for which one user wants to use.
You can save the token on device and next time user revisits your app, he won't be taken through this entire process, if token is valid.
I am working on an alarm feature for my application where, at a certain time, an alarm Activity is started, which subsequently starts either a vibration or a ringtone. The problem I've run into is that the vibration or ringtone is not stopped if the user presses the Home button.
I have overriden onKeyDown so that the alarm is stopped when a button is pressed, but that does not intercept the Home button code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (mVibrator != null)
mVibrator.cancel();
if (mPlayer != null) {
mPlayer.stop();
mPlayer.release();
}
return super.onKeyDown(keyCode, event);
}
How can I go about stopping the vibration/ringtone when this occurs?
I'd suggest stopping the alarm in onPause(). That gets called whenever another application comes to the foreground (including the home screen).
you can't intercept the home button - it's there for the reason that any app should allow the user to press home to immediately go to the home screen. you should consider stopping the alarm in your onStop() if you want the same effect.
the Home key event is intercepted by the framework and is not passed to the application layer,so you can't monitor this event.
my solution is that you can start a dummy activity (no status bar, no title bar, with a transparent background, so it looks like that there is no activity running).
within that activity , override the onPause() method to stop ringing or vibration. so when user press the home key , it would be called.
Just my idea. possibly there are better ideas.