Cordova - detect that app is in the background - android

I have a feature in my cordova app that lets user do something when user shakes the phone (I'm using shake.js for the purpose). The phone vibrates when it happens.
The problem is that, when I "alt tab" away from the app and it's in the background, the shake/vibrate combo still works. So the user might be using a completely different app and the phone would still vibrate.
Is there a way to detect that the app is in the background, or better yet, freeze the app somehow when it's going to the background?
I have this problem on Android (haven't tested it on iOS yet).

You can simply bind the pause event and you remove the shake feature from your app.
According to the documentation The (pause) event fires when an application is put into the background.
You can listen for the resume event to put back the shake feature.
As usual in the documentation you can find complete code examples.

Related

Reopen my Flutter app in background on FCM message

all day I already despair of a waking up system for my VoIP app. I must have already read over 100 articles and documentation about it, however I have not found a solution that works for me.
Use case:
My app is running in the background. That means the home button was pressed
after my app was started and the device is in the home screen. As soon as a notification arrives, the app is supposed to open again on a certain route because a call is coming in.
In the lock screen I managed this pretty easily with flutter_local_notifications and within the app it was of course no problem either.
Note:
Only Android is important, the rest can be neglected
Android version 10+
Flutter version 2+
I appreciate any input on how to solve my problem and have a good week.
Kind regards,
Shiro

Listening for a change in the foreground task

I am new to mobile development so please cut me some slack if I am missing something obvious.
Currently I am seeking a way to monitor a foreground task on iOS and Android. For example I want my program to do the following:
(Assuming my app is running in the background),
App "x" enters the foreground => Start timer in my app
App "x" leaves the foreground (either enters background or terminates) => Stop timer.
I have already looked through many cordova plugins but could only find code that monitored system notifications. I am looking for a way to listen for a change in the foreground application in the least cpu intensive way.
I also understand that iOS has this feature built in. For example, it is possible to see "Screen time" for each application over the past 24 hours or the past 7 days. I'm not familiar with native code for either iOS or Android but I am not opposed to learning.
My question is, how can I listen to changes in this "foreground process" given my current set up of ionic/cordova?
If you need any more information about what I am using or if I have forgot something crucial please let me know. Thank you.
Well there are lifecycle events in cordova that you can use in that kind of needs. deviceready, pause and resume events. pause is fired when your app is moving to the background, resume when app is moving to the foreground. You add listeners to this events. In case of events activation, you can read current time (by for example JavaScript Date object) and then compare it to each other. For more info look at: https://cordova.apache.org/docs/en/latest/cordova/events/events.html

Titanium APP (Android) crashes when brought alive from background

From the beginning of the APP development, I found that if I re launch the APP, the APP crashes and nothing gets displayed. I think it has something to do with the resource that is assigned to the APP and it trying to restore its previous state and it is failing to do so.
I see that, in many APPs, when it re launches the view is reset to its first screen. I would like to do the same in my APP.
Any suggestions on how I should be handling this ?
clear up memory ?
attach an event before-close and on-launch (or on-relaunch) ?
reset APP view to home screen ?
This is the first APP I am building (happens to be in Titanium). Therefore my fundamentals of APP development are weak.
Any help would be greatly appreciated.
I'm not sure what you are doing in your app, but I'm not seeing that in my Titanium apps. My users probably leave my app running/paused for days and then resume them as needed. Until their device runs out of juice, I'm pretty sure they never restart my app. Perhaps a module you are using?
There are events you can handle that will allow you to take an action when the app is put paused and when it is resumed. You can write your code to reset the app to the starting screen, which I'm partial to that idea as well. I'm not aware of a call you can make that will essentially restart your app.
The events you can handle.
Ti.App.addEventListener('pause', _.bind(this.exit, this));
Ti.App.addEventListener('close', _.bind(this.exit, this));
Ti.App.addEventListener('resume', _.bind(this.resume, this));
You would have to write the action taken when these events happen.
Code is from the https://github.com/appcelerator/Field-Service-App. This app has the hooks, but doesn't implement any actions for them. Check out the alloy.js file.

Keep android application open

On the Android Market there is an app called Sleep Timer, and it is a type of alarm clock that brings runs the alarm even though you locked your phone while on facebook. I made a type of app that detects movement however it only works if the phone is left on that app... How can I make it work when the app wasn't left upfront, but is still running in the background?
You should take a look at Android Services, which provide the functionality you seek. Basically they enable you to create components that run in the background even when the user switch away from your application.
You can find a very good introduction to them here: http://developer.android.com/guide/components/services.html

Check volume button usage when screen is off

For this question I'm going to quote another user who got no response to their question:
I've written an Andoid app that uses the hardware Volume buttons for another purpose.
It works fine if the app is running and visible, but when I turn the
screen off or let it time out, the button clicks don't get into my
handlers.
Does anyone know if there is a way to detect these button clicks when
the screen is off?
Source: AV695's question
I'm working on an app myself that makes use of the volume buttons, but as this user also noted, the normal behavior of checking buttons with onKeyPress stops working once the screen is off. This is because the Activity gets paused on screen off.
Is there a way to keep the activity running while the screen is off, or check for the usage of the volume buttons when the screen is off? I tried using a Service for this before but it's impossible to check for the volume keys like that as noted by Commonsware.
I doubt that this is supported (without resorting to a battery-draining wakelock) at either the platform, kernel, or underlying radio firmware levels without modifications to the last to bring volume presses during sleep to the attention of the kernel.
Within the realm of reasonable system-ROM modifications, a more reasonable one might be to modify an existing open source ROM for the device to insert some custom platform level code into the handling of the power button usually used to wake up the device preparatory to unlocking it - that at least we know does get the attention of the kernel. That code could then inform the user by sound or vibration if there are unacknowledged notifications.
You could optionally wait briefly, check device orientation, or look for another key press to avoid doing this in an annoying way when the user is holding the device outside their pocket and trying to unlock it.
Or you could not use the volume key and just set a timer to wake up every 15 minutes and vibrate if there are unacknowledged notifications, avoiding the need to fumble in ones pockets.
You mention it's a custom request: if implies it's one off or low-volume, another option to consider would be that a few vendors have "bluetooth watches" out with an SDK that lets you push notifications from an android device.
If you can capture the notification when it's generated, you could push it to the user's wrist, and then let the phone go back to sleep.
You cannot intercept the key while your application is in background, but instead of listening to the KeyPress itself. You can register a ContentObserver, as described in this question.
As Chris Stratton mentioned, the only way to keep your App alive is by using battery-draining wake locks.
However, since I found myself in the same situation, I came up with another solution. Unfortunately, you'll need a rooted device as well as the Xposed framework.
With Xposed, which replaces the zygot process so you can hook yourself into any constructor and method of the system, you will be able to catch the raw KeyEvents before the system handles them.
This is done in PhoneWindowManager.interceptKeyBeforeQueueing(). By using a XC_MethodHook, you can use beforeHookedMethod() on the afore mentioned method to catch every hardware button event, even if the device is in deep sleep.
After catching events you are interested in, you can create a temporary wake lock to do your things but don't forget to release the wake lock after you finished your work.
A good example of how to accomplish this is the Xposed Torch Module.
If you, however, rely on a non rooted system, the bad news is that it's simply not possible without draining the battery...
I was also trying to implement volume button press detection in my app and I left that part to be developed later once the core part is done. I was able to detect volume key press while screen is on even when phone is locked, from a background service.
Background Video Recorder 2 (BVR2) (and possible BVR1 also, I did not try) is one of the apps that can detect volume key press even when screen is off. While trying to implement volume key detection while screen is off in my app, I installed BVR2, hoping to find how it works. To my surprise it gave my app the ablity to detect volume keys even when screen is off. My app had a ContentObserver to monitor volume changes, but was not working when screen is off. When BVR2 is active my app also could detect volume key press when screen is off. Still digging.
But BVR2 has its own trigger action, that is to record video, an action you may not want to occur just for the sake of you application detecting volume key presses.
Another app is QuickClick. This app can give your app what it lacks, the power to detect volume key presses even when screen is off, without extra unwanted actions. Just install QuickClick and do not configure any action. Create a ContentObserver to monitor for stream volume changes and you are ready. You app will now be able to detect volume key presses even when screen is off.
Please note that my app runs as a background service.
Both of the apps mentioned above are meant for other uses, but uses volume key detection to perform action. I am in no way connected to any of the apps mentioned.
If these apps, and possibly dozens others, can detect volume key press, it can be done. I request experts to find out how to do it, so that we can implement in our app without relying on another app.
If you find this answer useful, please up-vote.
I am not sure if it is as simple as this but check this android blog:
Allowing applications to play nice(r) with each other: Handling remote control buttons
It explains the usage of a broadcast receiver that receives the up/down volume controls and other music controls.
In summary you should use registerMediaButtonEventReceiver

Categories

Resources