I'm trying to make alarm program, that will start a page when it goes of. Main problem here is that it is not trowing an error but method OnRecieve()
of AlarmReciever
class is not starting in time. Also i dunno how to check whether pendingIntent
is cancelled or not. And how to get this pendingIntent
to cancel if program is restarted.
There some code i tried:
MainPage:
using Android.App;
using Android.Content;
namespace TAlarm;
public partial class MainPage : ContentPage
{
public PendingIntent pendingIntent;
Android.Content.Intent intent;
public AlarmManager alarmManager;
public MainPage()
{
InitializeComponent();
}
private void OnTimerClicked(object sender, EventArgs e)
{
TimeSpan time = timePicker.Time;
intent = new Android.Content.Intent(Android.App.Application.Context, typeof(AlarmReceiver));
pendingIntent = PendingIntent.GetBroadcast(Android.App.Application.Context, 0, intent, PendingIntentFlags.Immutable);
intent.PutExtra("pendingIntent", pendingIntent);
alarmManager = (AlarmManager)Android.App.Application.Context.GetSystemService(Context.AlarmService);
DateTime startTime = DateTime.Today.AddHours(time.Hours).AddMinutes(time.Minutes);
long interval = 60 * 1000;//AlarmManager.IntervalDay; 60 *
alarmManager.SetRepeating(AlarmType.RtcWakeup, startTime.Ticks, interval, pendingIntent);
text.Text = "Lets start!";
}
private void Button_Clicked(object sender, EventArgs e)
{
var isPendingIntentValid = (pendingIntent != null);
if (isPendingIntentValid)
{
textMain.Text = "The PendingIntent is still valid";
}
else
{
textMain.Text = "The PendingIntent is no longer valid";
}
}
private void OnCancelButtonClicked(object sender, EventArgs e)
{
// Cancel the PendingIntent
alarmManager.Cancel(pendingIntent);
}
}
AlarmReciever:
using Android.Content;
namespace TAlarm
{
class AlarmReceiver : BroadcastReceiver
{
public override async void OnReceive(Context context, Intent intent)
{
var m = (MainPage)Microsoft.Maui.Controls.Application.Current.MainPage;
var nextPage = new NewPage();
var navigationPage = new NavigationPage();
await navigationPage.PushAsync(nextPage);
m.alarmManager.Cancel(m.pendingIntent);
}
}
}
I've tried to change:
var navigationPage = new NavigationPage();
into:
var navigationPage = Microsoft.Maui.Controls.Application.Current.MainPage as NavigationPage;
But nothing changed.
So this code should start nextPage when alarm goes off.
All kind of help will be appreciated!
Update: here is added receiver in AndoidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true" >
<receiver android:name=".AlarmReceiver" />
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<!--<uses-permission android:name="android.permission.REQUEST_SCHEDULE_EXACT_ALARM"
android:maxSdkVersion="29" />-->
</manifest>