How to continue execution when mobile device sleeps? - c#

I'm developing an application that needs to execute until a count down. When the handled turns off the screen, the countdown halts. How can I continue the execution when this situation happens?

I assume you mean that you want your code to continue executing after the device suspends? First off, you can't. When the device suspends, the processor stops running. You have a couple options though. You can periodically call SystemIdleTimerReset to prevent the device from suspending, run it in "unattended mode" so the backlight shuts off but the device does not suspend, or use an API like CeRunAppAtTime to preiodically wake the processor to run your code.

Note that there's an article on CodeProject.com that contains examples of managed (C#) code for using the Windows Mobile powermanagement features.
http://www.codeproject.com/KB/mobile/WiMoPower1.aspx
Additionally information on starting an application due to various conditions from managed code can be found in a CodeProject.com article too.
http://www.codeproject.com/KB/mobile/WiMoAutostart.aspx

There is another way - some "cheating method": Disable sleep mode in Windows Mobile 6. It uses SystemIdleTimerReset and additionally simulates a key touch.

Related

Xamarin Android - Periodic task execution

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.

Xamarin iOS - SignalR stops receiving messages in background mode

Currently i am developing an app which needs to be capable of receiving and interacting based on realtime event notifications from all platforms. We are using SignalR to achieve so. I am using Xamarin.Forms as this is a shared project.
Right now everything is working fine especially android is perfect. No matter application is in background or foreground its receiving messages from SignalR and playing sound and vibrates as expected. In iOS 10.3 simulator also its working as expected. Untill yesterday when i realized in real device when application goes in background mode it pauses everything and when its back in foreground it resumes as its supposed to be doing and as if the notification was sent just now. So i did some research and thought i would perform a long running task (in my case a forever loop with a 10seconds interval) in the background thread and in that way i was able to receive signalr messages (which i can see on the debugger) and vibration was playing while being in background. But after a certain time i am guessing within 10 mins iOS is clearing up its memory and when i open the app it seems reloaded because it shows the login screen instead of where i left the app.
Now question is:
1. How can i keep receiving SignalR events while being in background?
2. How can i stop iOS to reload my app due to being in background for a long time?
3. The app has to be Appstore publishable as in appstore must not reject it due to some dodgy works being done in the background like playing silent audio forever or using VoIP for background since this is not a VoIP app.
4. Also soon i am gonna be implementing location update in background mode so is this gonna keep the whole app alive in background? And behave like i am expecting?
As i am currently travelling i can not provide any code snippet but any questions please feel free to ask.
Thanks for your help and reading this boring article.
Just in case if someone is facing similar issue.
Apple doesnt allow to execute your app unless it falls in certain category Look at table 3-1. Fortunately, I had a valid reason to use continuous location update in foreground and background therefore as soon as I implemented location update event all my problems were solved immediately.
How to implement location update? Location DependencyService example for Xamarin.Forms
Hope that will help.

Listening to "Triggers" and reacting to them in Windows Phone

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.

Programmatically set wake on time

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

How to stop a PDA sleeping

I have an application for PDAs with a long running process, and I'm getting the problem that the PDA is going to sleep before the process has completed.
I haven't got any control over the power settings on the PDA, does anyone know a good way for my application to stop the PDA from going to sleep?
Many thanks
Have a look at:
http://www.pinvoke.net/default.aspx/coredll.SystemIdleTimerReset
You need to make a function that calls SystemIdleTimerReset, SHIdleTimerReset and simulates a key touch. Then you need to call it on a regular basis from within your application.
For an example in C++, look here:
Disable sleep mode in Windows Mobile 6

Categories

Resources