I'm writing an app with Xamarin.Android to retrieve data from some domotic sensors(Sonoff, Shelly) and to set some switches. I need it to make api calls even in background, say about every 5 or 10 minutes. I know it's a very short amount of time, I plan to do this with an Arduino later, but now I need this.
I researched a bit, read about Services and their limitations, but I can't figure out how to do this. Can someone help me? Thanks
You can either:
create a Foreground Service that keeps running, and you execute a timer there
use WorkManager from Android X, which depending on device capabilities and software versions will use the following in priority:
JobScheduler
Firebase JobDispatcher
AlarmManager
JobScheduler allows you (kind of like cron on Linux) to set up scheduled tasks to be run. However, it is limited in how frequent you can do this. This frequency depends on Android version and depends on the criteria on the job and whether the device is not running out of resources.
With the foreground service, you can do more or less what you want as long as the service is alive. You might need to disable battery optimizations in battery settings for your App, but otherwise it should just work.
Apart from these features, unless you are either using a rooted device or you are device admin on the device, you can't do much more. In case of root/device admin, you can mark your app as Persistent to disallow it being killed by Android.
Related
I'm trying to convert an Android app to Windows 10 UWP. On android its easy: when boot completed, app service is started. It connects to controller over internet, fetches system state and all data (temp sensors, pumps, valves, etc) and keeps everything in memory. Foreground app can get data as soon as service gets them and display values, charts realtime. After closing foreground app, service keeps working, I still have all system state and I can play alarm sound if needed.
Is it possible to do [almost] same functionality on Windows 10 uwp?
I cannot find a way to start service with windows. Service started with foreground app is stopped when foreground app is closed. SocketBackgroundTask keeps connection perfect, but system state is lost with service.
Should I save system state to file and analyze all data after each renew? Data flow varies from once in 10 minutes to ~10 per second.
Or should I forget Windows 10 as limited platform?
I am designing a similar piece of sensor control software, and I have found UWP/Win10 to be limiting. We ultimately resorted to using Assigned Access to keep the app permanently in the foreground.
Assigned Access
Assigned access assigns an app to an account. So when Mr. Bob logs in, the app starts full-screen, and it cannot be closed, and if it crashes, it is restarted.
Note that the only way to access other parts of the system is to hit ALT-CTRL-DEL and log in as a different user. That might be bad for some, but if you have critical process monitoring going on, then it's probably a good thing that the user can't mess about with the system or quit the app.
It's also quite simple to implement, you only need to add a declaration to the app manifest, and you need Win10 Pro or Higher.
Windows IoT
You could also look at Windows 10 IoT, when you deploy an app to it, it does pretty much the same thing. However the range of hardware is quite limited, and many of them aren't fully functional yet - RPi suffers from SDcards being inherently unreliable, and lack of graphics acceleration. Dragonboard lacks driver support for resolutions other than 720p, etc. https://developer.qualcomm.com/forum/qdn-forums/hardware/iot-development-platform/29652
Extended Execution
In addition we have experimented with using extended execution, which lets the app run in minimised state, potentially indefinitely. I have mixed feelings about it. Although the app will keep running most of the time, but if the OS is struggling for resources, the app will get suspended and won't be restarted until the user switches back to it.
Basically I'm working on a project where I need to:
Have a background service that can receive small packets of data from a paired bluetooth (BLE to be precise) device.
Based on the received data, perform various functions on built-in Windows apps (media player, camera, phone dialler etc.) like switch to the next/previous track, alter volume level, take a picture, call a number etc (basically control built-in apps).
Now there was an extensive hardware part to this project which I began working on assuming that Windows Phone would offer the same level of flexibility as Android and that the aforementioned tasks could be easily performed, but now that I searched around a bit and saw the constraints for Background Agents I was dumbfounded to say the least, and by the looks of it the kind of functionality I'm looking for is completely unavailable in WP APIs. And while I couldn't find a definitive answer regarding the second task I wish to perform, I'm assuming the sandbox-style environment which WP apps run in will prevent that from happening as well.
Is there ANY way the above mentioned tasks can be performed with a Windows Phone 8.1 app or even something close?
Short answer: no.
Long answer: for various reasons Windows Phone is designed to provide ultimate control of system functions to the user. In general, all actions have to be user-initiated; even the background tasks have to be ok'd at some point. Reasons include battery life, stability, predictability.
A related policy is that no app may interfere with the operation of another. The only way apps can communicate is via launcher tasks or sharing protocols. Using these you can do things like prompt the user to dial a number.
Out of the tasks you list, the only one I can see being a feasible project is if you write your own camera app or music app that integrates with your Bluetooth device. It's either/or though, you won't be able to make a one-stop app.
For my app I need to be able to let the user specify a given time and let my app run a background task at that specific time.
I understand that with wp8.1 a timetrigger background task can only run every 15 minutes. Is there anything I can do to ensure my app will run the task if the time has gone 10 minutes over the user's specified time?
For what I understand tasks are only executed in intervals multiple of 15 minutes in Windows 8.1 and 30 minutes in Windows Phone 8.1. Have a look here: http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh977059.aspx
I can not add a comment, that's why I share my experiences as an answer.
We had a similar problem in an application - we target the Windows Store 8.1 platform but our experiences can be relevant for you as the app cycles in the two platform are converged (http://msdn.microsoft.com/en-us/library/windows/apps/dn632424.aspx).
In our app we have to monitor streams and collect information. This logic has to be executed per minute what is not supported, as mentioned. That's why we use a simple Task with a CancellationTokenSource, and we start/stop the task in the app cycle-related events (Suspending, Resuming). This "solution" is not acceptable if the execution of the background logic is required when the app is suspended, too, but in our case this was not required.
There are apps on Android such as Tasker and Trigger that allows the user to set up some "Triggers" and the "Responses" that should execute when each trigger triggers.
The triggers could be, for instance: Connecting to a certain Wi-Fi, Arriving a certain location (Geo-Fencing), Plugging the earphone, tapping an NFC Tag and so forth.
I wonder if there's any way to to something like this on the Windows Phone platform. I have no background in developing for WPhones (even though i have a huge C# background), so i'm not sure whether the platform itself allows this sort of "background monitoring of sensors or, if there are such sensors (such as "headphone plug").
After a quick research, seems that access to some APIs are not allowed from Background Threads, i wonder if there's anything related to security here, or just an inability of the platform.
Is there any way to achieve what i want?
On Windows Phone 8 you can perform background processing, with Background Agents.
You can use Sceduled Taksto register a class containing a method that is called periodically, even when your app is not running. They are multipurpose and offer the greatest utility for extending your app to perform background activities.
You can surely display a message to the user, or fire an alarm or something, a message would be more suited, for location aware apps. Those registered as geographic location providers are able to continue running despite there being another app running in the foreground.
For the rest of your triggers, I'm not sure. You'll have to check the Sensors.
No, you can't hook any triggers or events. The only thing you can, as Pantelis said, is to create very limited PeriodicTask that may or may not be run every roughly 30 minutes and be constrained to max 25s of running time. This is deliberate platform limitation, AFAIK done mostly because of battery usage and security reasons.
If something can't be achieved by PeriodicTask, give it up. This is the most versatile background process, others are even more limited. This is by design to prevent daemons from taking over the phone.
We are creating a display system which we will be running on top of Windows 7 embedded.
The computers will not have keyboard or mouse, thus will have no direct user interaction.
Since these computers will only be used during certain times we would like to define schedules for them and make them power off (including their screens), and automatically power on again at specific times.
For this, we need to be able to set the bios WakeOnRTC timer from code. It is not sufficient to wake from hibernate or sleep, as referred to at Schedule machine to wake up.
I know from others that MythBuntu can do this, but that is Linux-based.
I've also heard this is easier to achieve with UEFI-bioses that are emerging.
At this point we are open to select any bios if anyone has a solution.
I'm not going to put too much because this question has already been answered, see the following links.
C#: How to wake up system which has been shutdown?
http://www.codeproject.com/Articles/49798/Wake-the-PC-from-standby-or-hibernation
In short the solution revolves around the following two Win32 API functions:
CreateWaitableTimer
SetWaitableTimer