I am developing an app that uses ws connection on KaiOS. When I press back and put the app in the background, somehow the app instance is not killed. I can still get message coming in from ws connection even though the app is in background. This makes the phone runs out of battery quickly because exited app remains in the runtime process I suppose.
But, I do need a situation where the app runs in background (not killed).
So, the question is, do we have access to kill an app process, for example when user press something or normally when app is closed and screen back to menu screen from app screen?
I think you can observe the Backspace key and close the window.
Such as:
function handleKeydownEvent(e) {
if (e.key == 'Backspace') {
e.preventDefault();
window.close();
}
}
document.body.addEventListener("keydown", handleKeydownEvent);
Related
I created an app using Xamarin.Forms. I am trying to detect if there is a change in phone's language each time the app is reopened after being closed using the back button. Tried to Debug.WriteLine on each of onStart, onSleep and onResume to see which one occurs when I open the app again but none of them worked. This is what i tried:
App.xaml.cs
protected override void OnStart()
{
Debug.WriteLine("onresume");
//CultureInfo language = CultureInfo.InstalledUICulture;
//Thread.CurrentThread.CurrentUICulture = language;
//ApplicationResource.Culture = language;
//Application.Current.MainPage = new NavigationPage(new MainPage());
}
protected override void OnSleep()
{
Debug.WriteLine("onsleep");
}
protected override void OnResume()
{
Debug.WriteLine("onresume");
}
How do I know when the app is reopened so I could try the language change code?
If i understand your concern correctly two things can help you:
On android you can have a very wierd behavour if you do not specify SingleTask for your mainactivty. Basically if you "close" the app using Back button and then click on app icon, the app is launched from start recreating App but keeps static variables and the debugger is still connected too. You can detect it setting a static variable, then checking it with a breakpoint inside App.cs constructor. So the avoid such just use LaunchMode = LaunchMode.SingleTask inside the MainActivity.cs [Activity] attribute. This way after you return to the app after the Back button + click on icon you'll get a fresh new app with OnStart invoked.
Some could say to use LaunchMode.SingleInstance but it will kill your app if you click on push notification whie app is active so better use SingleTask, it will open smoothly the running app.
To store and retrieve data between app launches use local persistent storage, full docs: https://learn.microsoft.com/en-us/xamarin/essentials/preferences?tabs=android. So when you got your language store it, then at app start (App.cs constructor) check values.
I had done a simple to test your code with the Android simulator. And I use the Console.WriteLine instead of the Debug.WriteLine.
I had found that:
When the app exited with the back button, it will call the OnSleep method and if you get into the app in the recent task, it will call the OnStart method.
When the app go to background with the home button, it will call the OnSleep methed too, but if you get into the app in the recent task, it will call the OnResume method.
So if you add a break point in the OnStart method, when you exit with the back button and get into the app again, it will hit the break point.
I am trying to save data before application (WinRT 8.1) close/sleep/minimze (or windows shutdown/restart in tablet with WIN10) using app suspending event.
https://learn.microsoft.com/en-us/windows/uwp/launch-resume/suspend-an-app
However, it os not working on power off / shutdown WinRT/UWP: Is suspending action rising on long power off button holding?
The Suspending lifecycle event will fire in case of a normal OS shutdown - if you do Start -> Shut down.
This is unfortunately not the case with long power off button holding and restart button press, because both these are improper ways of shutting down your PC. Holding power button to shutdown essentially suddenly "cuts-down power" to the PC, which means the OS cannot respond to this and all unsaved data are lost. This method of shutting down a PC should be used only when something really bad happens and everything freezes. That is why the UWP app has no chance to run the suspending event handler in this case.
Is suspending action rising on long power off button holding?
System will shutdown forcibly with long press power off button. And the system could not make sure Current user session is finished. So the suspending event handler could not be invoked correctly.
From Windows 10 universal Windows platform (UWP) app lifecycle:
Current user session is based on Windows logon. As long as the current user hasn't logged off, shut down, or restarted Windows, the current user session persists across events such as lock screen authentication, switch-user, and so on.
So, before shut down, the app still in the Current user session. And Suspended will be invoked on power off(shut down).
Note, you can not test it in debug model within Visual Studio. Because when you shut down the system, Visual Studio will exit degbug model at first. The Suspended event will not be invoked as expect. You could verify with following code.
private void OnSuspending(object sender, SuspendingEventArgs e)
{
var stringBulider = new StringBuilder();
var deferral = e.SuspendingOperation.GetDeferral();
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
Object value = localSettings.Values["exampleSetting"];
stringBulider.Append(value.ToString() + "/Next");
localSettings.Values["exampleSetting"] = stringBulider.ToString();
deferral.Complete();
}
Each time you shut down, the stringBulider will be append one at a time.
Im trying to write a simple app for some running friends of mine. Basiclly they run for long distance and along the way they want to use the band to mark a location. Click on a tile on the band, click on a button that says "Mark" They do not want to worry about the application running or not. So I assume app will be closed on Android phones. I got my prototype working, kind-of. The main issue I'm having is app is closed!
BandPendingResult addTile(Activity var1, BandTile var2) throws BandIOException;
Add type first parameter requires an activity. I am trying to build this as a service that when a button on the Band is pressed "wakes" up the service, gets the GPS location and sends it to the server. Activity is not a service.
When the service starts I have tried to remember the activity its bound to, but when the application get closed the activity is killed off and BOOM!
Adding a Tile requires an Activity because it requires user confirmation (i.e. it displays UI). However, you should not be adding the Tile as part of your service; it should be done (just once) as part of the application's setup/settings flow. The service should only be concerned with responding to the Tile events.
I want to disable the close button on the window (main application window) when an operation starts so user can't exit the application and enable it again when the operation is complete. How can I do that in Qt application? My platform is windows 7.
Alternatively I could show a message if user press close button and exiting application that a process is running in the background and abort closing the application.
How can I do either?
If you want disable button, you can use next:
auto flags = windowFlags();//save current configuration
//your main configuration which do the trick
setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint );
//...
setWindowFlags(flags);//restore
If you are sure that such "feature" will not irritate your users, you can use this, in another case use link from the comment.
It has been stated that an app running a HKWorkoutSession will have special privileges over other watchOS 2 apps, so when a user looks at their Apple Watch, it will go to the view showing running a workout rather than the watch face.
Currently, on both my device and simulator, this is not the case. If I start a HKWorkoutSession and then leave for 5 minutes and then interact with either the Apple Watch, or the Watch Simulator, it presents the watch face.
If I then open my app, it appears to have been frozen, rather than terminated (which is what I imagine happens to other apps). As the UI will update when I need receive a response in my query.updateHandler. Also if I set it to provide haptic feedback every time my query.updateHandler receives a new HKQuantitySample it will do so, so the app must be running in the background in some form.
Has anyone else noticed this behaviour, and am I doing anything wrong, or expecting something I shouldn't?
Here is how I start my HKWorkoutSession:
self.workoutSession = HKWorkoutSession(activityType: HKWorkoutActivityType.Other, locationType: HKWorkoutSessionLocationType.Indoor)
self.healthStore.startWorkoutSession(self.workoutSession) {
success, error in
if error != nil {
print("startWorkoutSession \(error)\n")
self.printLabel.setText("startWorkoutSession \(error)")
self.printLabel.setTextColor(UIColor.redColor())
}
We're seeing that too, for the moment we've made sure 'opens last activity' is configured.
When the UI is active we start a dispatch_timer to request and process data in 1 second intervals.
Make sure you do any significant processing using the NSUserProcessInfo method though and pause the dispatch_timers whenever you are no longer active. You'll get crashes otherwise.