IBM Worklight - How to use Worklight in a background process - background-process

I have an application that should run continuously even after the user presses the back button.
It has to send some data using POST method to my database in a remote server for every half an hour. This should happen even after the user has pressed the back button and the app should only stop when the mobile is switched off.
While I have set up the HTTP Adapter to send POST data and also an HTML file to call the adapter procedures on launch, I dont know how to make it background or which code to run to keep sending the POST data every half an hour.

It sounds like you are running on Android.
The expected behavior in Android is that pressing the Back button QUITS the application.
Moving to the background is performed by pressing the Home button.
So, I don't think you should do this when pressing the Back button... or maybe you should at least present the user the choice of either quitting or moving to the background (you can override the Back button by using WL.App.overrideBackButton).
That said, I am not familiar with a programmatic method to move to the background instead of quitting (maybe there is an existing Cordova plug-in or another way that does this).
The other way to accomplish this is by using an Android Background Service, however, Worklight does not have support in its Native API to do this.
As for what happens when the Worklight application is in the background, I have never tried it myself, so I cannot be certain at all, but try using WL.Client.setHeartBeatInterval to keep alive the connection to the Worklight Server and write some logic that sends the adapter requests. See if that would work for you...
I have found this:
How to create plugin in phonegap to run the application in background?
Maybe you'll be able to massage it into your Worklight project (in case the heartbeat approach above does not work).
More information on running in the background:
Android:
http://developer.android.com/training/basics/activity-lifecycle/index.html
http://developer.android.com/reference/android/app/Activity.html
iOS:
http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

Override the Back button by using WL.App.overrideBackButton.,
remove the exit function in that method and leave it blank (if you want the background process not to exit when clicked)

Related

How can I close a browser tab knowing the process ID?

I have a process that adds tabs to Vivaldi (or any browser): one to an external url and one to a local html file. I am able to identify the process IDs associated with each tab.
I want to be able to close the tabs. I have tried kill <id>. That clears the page of the local file, but the tab is still there and can be reloaded if I refresh the page. kill has no effect on the tab associated with the external url.
Is there a way to do this?
Killing processes is the wrong approach here anyway because apart from causing unexpected termination and not orderly closing, nothing guarantees each tab to live its own process. You may have both of them living in the same process, or sharing a process with other, unrelated tabs. Bottom line, it's not going to work or at least it'll work only sometimes and cause collateral damage. (Others asked for such a way before.)
My suggestion would be a browser extension that uses native messaging. You could then ask it via the native messaging function to close certain tabs for you, using the officially supported tabs API that the browser exposes to extensions.
(These links are to the Chrome extension docs, but Vivaldi is Chromium-based as well and supports the same APIs.)
Alternative idea that works without an extension:
Tabs opened through the command line behave as if they were opened by a script of the same origin, insofar that the website in them is able to call window.close(). So depending on your use case, maybe you can arrange for the website in the tab to close the tab by itself.
If one of them is "external" in such a way that you can't control its contents, then you could instead have one tab open the other one through JavaScript, because then the first tab can close the second tab using close as well.
If you need a way to communicate to the website running in your tab(s) that you want it to close itself, you could also do something like starting a local server at a random unused port and passing the port into the website via a URL parameter1, and stopping the server when you want to close the tab. Then, inside your website you would regularly poll the local server URL using AJAX and close the tab when it fails2. (Remember to return CORS headers for this to work.)
This is just one of several possible ways, and yes it is a bit "hacky" - so I'm open to suggestions on how to improve on this idea.
Another alternative (which may or may not fit to your use case): Instead of opening a tab, you could open a separate popup window for each website using --app in the command line before the URL. Then you could find the corresponding window by checking what is the newest window with a matching title, and you could close it programmatically (check out xdotool and xwininfo).
1: Why not a fixed port number? Because you can't control whether something else is already listening on that port on the user's machine.
2: Why not the other way round, starting the server in order to close the tab? Because then you would have to wait to ensure that the website noticed that you started the server, and if you would stop the server too early then the tab would never close, so it's extra effort and an extra possible failure point, for example if there is high CPU usage at the moment or Vivaldi put the tab into sleep mode in the background. Additionally, with my method, killing your "manager" process would then also cause the tab to close instead of leaving it sticking around. And, finally, you don't want another process to interfere with your communication by opening a server on the same port that you chose before you do so, so it'll be best to open the server right away and not only once you want the tab to close.

How do I write a background service that will work if the app is cancelled

I'm writing an Xamarin Forms App(C#) that has a UI, which takes data from a user and passes it's data to a background thread/service to be sent to a server.
The datafirst gets saved to a local database for safe keeping. (Cleanup will be later)
I need a service or something that will guarantee delivery of the data, even if the application is minimized or shut down (swiping it off the screen).
Am I looking for something that does not exist?
I have tried an IntentService, which seemed to do what I wanted but stopped if I killed the UI.
I tried a real Service, which also stopped if I killed the UI, but I may not have set it up properly.
Also, a service seems to run on the UI thread. What good is that? I can't get it to run as a BackgroundWorker either using StartService inside the worker, so I tried running the service as normal and in the StartCommandResult OnStartCommand, calling the data fetch/save method as a Task or Thread. It works as long as the app stays active.
I looked at AsyncTask too, as well as AlarmManager.
What am I missing???
Checkout the background jobs functionality in the Shiny Framework - it's specifically built for use cases like yours
https://github.com/shinyorg/shiny
Full how to video with the creator here
https://www.youtube.com/watch?v=aLtk-VlGicY

Hololens Simulate gestures

Im running server on hololens, and i created a PC app that works as a client.
Via client i can drag, rescale, rotate etc. holograms created in unity.
The holograms have some function that starts when user makes an air tap gesture on them.
I want to be able to start those functions in my pc app.
The easiest way to do it is just to send an info from app to hololens to do certain thing but...
I want to run those functions specificly as if the air tap gesture happened.
To do so i need to simulate an air tap gesture.
I want to send the coordinates from pc app to hololens, and i want my scene to behave like there was an actual air tap gesture executed without executing that gesture in real life.
Similiar thing can be done on PC version of Windows10 and its described for example here
https://superuser.com/questions/159618/simulating-mouse-clicks-at-specific-screen-coordinates.
There is my question to you.
Is it possible to simulate gestures? I would realy appretiate any info you can share with me.
Thanks.
If you are using the standard MRTK Gaze stuff to handle your gestures, you could define a new IInputSource.
The example for adding Gamepad input could be a good starting point - instead of triggering an air tap when a gamepad button is pressed, trigger it in response to remote calls from your PC app.
The benefit of this is that it's in keeping with the existing input system - your code which acts on input doesn't need to know that it came from a gamepad, a hand, or your desktop app.

In Electron, can I access the console in a renderer process from the main process

I am working on an Electron app where I'd don't control the contents of the render process. For this portion of the app, I'm just browsing a remote URL outside of the app.
I'd like to be able to stream the console from that render process to the main process and detect the presence of certain messages and act upon them in the main process.
Since I don't control the render process I can't use IPC to send messages. If I launch Electron with the ELECTRON_ENABLE_LOGGING environment variable, I can stream the render process, but only to the terminal. I don't know how to access that data in the main electron process. Is this possible somehow?
Best shot would be using console-message event in webContents. (https://electronjs.org/docs/api/web-contents#event-console-message)
It allows to hook console messages from certain webcontents' console, but mind there is one known issue of param for those consoles are not forwareded: i.e console.log('message', ...args); you'll likely not able to grab args.

Detecting browser window close event in flex?

Is it possible to detect in Flex application browser window close event so that an action can be
started when user closes Flex application, does anyone know how to do that if it's possible in the
first place? The reason why i am asking this is because i have a multiuser Flex application where
every user has it's own directory on a server side. Application has logout button which triggers
cleanup of user's directory but what if the user just closes the window? I would like to be able
to lunch that same cleanup upon browser close window
In the page hosting your app, write a Javascript function triggered by window.onbeforeunload, and this function can call a function inside your Flex application.
Note that the onbeforeunload function is not guaranteed to work for all browsers.
I would not recommend that approach because the closing action fails too often, meaning worthless. My browser freezes and force-quitted several times a day. My computer sometimes freezes. My internet connection sometimes dies. I think, some browsers even do not guarantee those kinds of actions executed every time.
So, the session timeout might be one safe way in most cases.
You can also try having a socket connection, so that your server can ping if a user is alive and also can detect if socket is closed. Even socket, however, can be unresponsive or can be disconnected sometimes while user is still using the application.
You might want to be strategic.

Resources