Hi,
I have a timer in one of my activities that shows an alert dialog every minute. This works fine on the activity it is created on but what I need is it to continue showing every minute despite what activity I have open.
Any ideas how I'd do that?
Heres my timer and alert dialog.
private void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
{
okbuttonpressed = false;
Activity activity = this;
Console.WriteLine ("Alert");
Android.App.AlertDialog.Builder builder = new AlertDialog.Builder (activity);
RunOnUiThread(() =>
alertDialog = builder.Create ());
RunOnUiThread(() =>
alertDialog.SetTitle ("Welfare Alert"));
RunOnUiThread(() =>
alertDialog.SetMessage ("Alert triggered"));
RunOnUiThread(() =>
alertDialog.SetButton("OK", (s, ev) =>
{
okbuttonpressed = true;
}));
RunOnUiThread(() =>
alertDialog.Show ());
}
Answers
Create a BaseActivity class which contains the implementation you want every activity to adopt, then ensure all your activities extend your BaseActivity.
Also you can refactor your RunOnUiThread calls as follows:
A good way to keep track of this is to create a central class to track what activity you are currently and also to run your timed thread in. Here's an example:
`using System;
using System.Threading.Tasks;
using Android.App;
using Android.Views;
namespace XamarinAndroidForum
{
public static class App
{
// Keep track of what current activity you are on here
public static Activity _currentActivity;
private static Task _alertLoop;
}`
To start the thread, in your MainActivity you will need to add the following to your 'OnCreate' override method:
App._currentActivity = this; App.RunAlertThread();
and then add this to your override 'OnRestart' method:
App._currentActivity = this;
The reason for that is the OnCreate method doesn't fire if you navigate back to MainActivity with the back button. You will need to include App._currentActivity = this; in OnCreate override methods throughout your activities so that the thread knows what activity to alert on.
Also, I would keep a minimal amount of code in your RunOnUiThread as this can bog down the UI and you want to run as much code externally from the UI thread as you can.
Thank you for your answers.
@hobeau your example is great thanks, I have another time execute every 30 seconds that checks for the ok button clicked if not a welfare alert call and text are sent.
How would I incorporate those into this code?
private void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
{
okbuttonpressed = false;
Activity activity = this;
Console.WriteLine ("Alert");
Android.App.AlertDialog.Builder builder = new AlertDialog.Builder (activity);
RunOnUiThread(() =>
alertDialog = builder.Create ());
RunOnUiThread(() =>
alertDialog.SetTitle ("Welfare Alert"));
RunOnUiThread(() =>
alertDialog.SetMessage ("Alert triggered"));
RunOnUiThread(() =>
alertDialog.SetButton("OK", (s, ev) =>
{
okbuttonpressed = true;
}));
RunOnUiThread(() =>
alertDialog.Show ());
bTimer = new System.Timers.Timer();
bTimer.Elapsed+=OnTimedEvent2;
bTimer.Interval= 30000;
bTimer.Enabled=true;
}