When a mobile app is closed it generates an event that can be captured.
// trap app shutdown event
Titanium.App.addEventListener(‘close’,function(e)
{
Ti.API.info(“The application is being shutdown”);
});
I would like to create an event handler that pops up an alert to ask "Are you sure you want to quit the app", just in case someone presses the wrong keys...
Is it this possible and can you cancel the shutdown event?
You can do it in Android only. It's not possible on iOS.
Further, on Android, this can only be done if app is closed by pressing back button, not from task manager.
Here's how you do it on Android:
<Alloy>
<Window id='win'>
</Window>
</Alloy>
// for Ti SDK 6.0.0.GA or above, you need to use this code
$.win.onBack = close;
// else use this code for SDK 5.5.1 or below
$.win.addEventListener('androidback', close);
function close() {
alert('Do you want to shut down app????');
}
Read about the androidback event here
And the ticket which added the above breaking change:
https://jira.appcelerator.org/browse/TIMOB-19919
Short answer is: No, you cannot
You can track events related to the lifecycle of the application but you cannot prevent that from happening. Without getting into the bad user experience that this could be let's talk about a user scenario will look like.
If you are using your Android phone and you want to close an app and it just keeps asking you if you are sure you want to close it, the programmer ignored the Yes action and keeps the app running. How insecure and annoying will this be?
I managed to resolve this problem by firstly hooking into the Android back event and secondly by using bencoding Alarm Manager to run the background code constantly. That way, if someone shut the UI down, the app continued to run in the background regardless.
Related
I learned that following is Overview screen.
I clicked the dialer and dialed a number and then swiped the dialer to the right.
But still the call is on like this.
How is this done?
I am a newbie to Android programming. I am thinking if they had used a back ground thread or a separate service or something else, so that it is never killed accidentally. How is this done?
Please help me. Thanks.
I believe it is using the Android foreground service. You can checkout how to run it in the doc:
https://developer.android.com/guide/components/services.html#Foreground
The difference of foreground from background service is when phone is low on memory, system will not automatically close it to release memory, it should be closed manually by user, which is why you need to have a notification as a way for user to close it when you first create it.
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.
I am makin an android application on Emergency communication.I want my application should run all the time and whenever it needs it should be able to run.I want that user should not be able to close it by any means.Like Google Maps application which is restarted again on killing its all activities and even we force close it,it will be restarted.
Seems you are learning app development ... but this is not the way you should pose question here... you should post what you have done and code issues and not ask for an entire class...
But i will try to answer your question are you trying to create an app which when started wont be able to be closed and come back to home screen ???!!!!!
See you are creating an app which will run on android operating system which is similar to linux so you must follow the Activity
LifeCycle
I remember i created an app where there were shortcuts on screen so using that i was directing the user to specific links but you can use for calling etc.. i donno if that is what you are searching for...
You might create a notification bar if you want which cannot be cancelled and you can place the code in the intent which will be called from this...
Also services are there which u can utilized what other users have suggested...
thx
You could use an extra service running in the foreground, but there will be always a notification of this service in the notification bar.
See here: http://developer.android.com/guide/components/services.html#Foreground
I'm having the follow question how I'm disable Force Stop button, this is to accomplish one task of my project this is not for virus or malware or malicious things. I have to disable it for my service do not be killed because inside him it checks the time and create a file in a determinate time of system this is because my client wants to block the Android OS when it creates. I've already searched and appears if you start the with startForeground() method the force stop button became disable and another solution but i do not find much material is about create a system service if anyone know i will be happy. And if want make a suggestion for my work and works i will be very grateful. Thanks
You can't. The user is always able to stop a service on the phone, as he should be- its his phone, he can decide he doesn't want your app to run anymore.
The force close button appears because your program has entered a state in which it can no longer be executed sensibly (for instance an uncaught exception). Disabling the display of the close button will not solve your problem, you should debug your code.
Can someone point me in the right direction to have an Android Application reopen itself to the forefront after a user has pressed the home key? It should be able to be linked to some kind of countdown (which I already have setup, and runs when pushed to background).
I just can't find the function to force this action
*And yes, this is what the user will want, don't worry, I know this doesn't sound user friendly (forcing open) but in this case the user wants it.
Thanks
I needed the exact same thing before -> an app being displayed upon an event but I was sure I read on the Android developers site that this can't be done and that's what notifications are used for, although starting a new activity does bring an app to front. I found this link however to a NEW_TASK_LAUNCH flag with startactivity that might solve your problem -
http://developer.android.com/guide/appendix/faq/framework.html#4