AlarmManager class has been around for quite some time, given that it was added in Android API Level 1. Deriving from java.lang.Object, alarmManager class resides in the android.app package.
AlarmManager is important if you want to schedule code to be executed in the future. In this example, that code to be executed in the future is just showing of a simple toast message.
We schedule showing of a toast message. The user will enter the time in seconds in an edittext after which the message should be shown. The alarm then rings after that specified time and we show our message.
You can find more details about AlarmManager here.
Screenshot
- Here’s the screenshot of the project.
Android Alarm Manager Example
Common Questions this example explores
- Android AlarmManager in xamarin.
- What is Xamarin Android AlarmManager?
- How do I schedule work to be done in future in xamarin android?
- Easy alarm manager example with a toast.
Tools Used
This example was written with the following tools:
- Windows 8
- Visual Studio IDE
- Genymotion Emulator
- Language : C#
- Platform : Xamarin Android
Source Code
Lets jump directly to the source code.
MyReceiver.cs
- Our Broadcast Receiver class.
- Make it extend BroadCastReceiver.
- We then override the OnReceive() method. This is where we write the code to be executed when alarm rings.
- In this case we simply display a toast message.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace Alarm_Manager
{
[BroadcastReceiver]
public class MyReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Toast.MakeText(context, "Alarm Ringing!", ToastLength.Short).Show();
}
}
}
MainActivity.cs
- Launcher activity.
- Main.axml inflated as the contentview for this activity.
- We initialize views and widgets inside this activity.
- We also initialize and start our alarm inside here using the alarmmanager object.
using System;
using Android.App;
using Android.Content;
using Android.Widget;
using Android.OS;
using Android.Views;
using Java.Lang;
namespace Alarm_Manager
{
/*
* OUR MAINACTIVITY
* -Extends Activity
* -Initializes our views and widgets
* -Starts Alarm
*/
[Activity(Label = "Alarm_Manager", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
//DECLARE WIDGETS
private Button startBtn;
private EditText timeTxt;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
this.initializeViews();
}
/*
INITIALIZE VIEWS
*/
private void initializeViews()
{
timeTxt= FindViewById<EditText>(Resource.Id.timeTxt);
startBtn= FindViewById<Button>(Resource.Id.startBtn);
startBtn.Click += startBtn_Click;
}
void startBtn_Click(object sender, EventArgs e)
{
go();
}
/*
INITIALIZE AND START OUR ALARM
*/
private void go()
{
//GET TIME IN SECONDS AND INITIALIZE INTENT
int time=Convert.ToInt32(timeTxt.Text);
Intent i=new Intent(this,typeof(MyReceiver));
//PASS CONTEXT,YOUR PRIVATE REQUEST CODE,INTENT OBJECT AND FLAG
PendingIntent pi = PendingIntent.GetBroadcast(this,0,i,0);
//INITIALIZE ALARM MANAGER
AlarmManager alarmManager= (AlarmManager) GetSystemService(AlarmService);
//SET THE ALARM
alarmManager.Set(AlarmType.RtcWakeup, JavaSystem.CurrentTimeMillis()+(time*1000),pi);
Toast.MakeText(this, "Alarm set In: " + time + " seconds", ToastLength.Short).Show();
}
}
}
Main.axml
- Content Layout.
- Defines the views and widgets to be displayed inside the MainActivity.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#009688"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/timeTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:textSize="25dp"
android:textStyle="bold"
android:ems="10"
android:hint="Number of seconds"
android:inputType="numberDecimal" />
<Button
android:id="@+id/startBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="60dp"
android:layout_marginTop="120dp"
android:text="Start" />
</LinearLayout>
Video/Preview
- Video version of this tutorial.
Download
- Download the Project below:
How To Run
- Download the project above.
- You’ll get a zipped file,extract it.
- Open the Visual Studio.
- Now close, already open project.
- From the Menu bar click on File >Open> Project/Solution.
- That’s it.
Conclusion.
We saw a simple xamarin android alarm manager example.
More
YouTube
- Visit our channel for more examples like these.
- Lets share tips and ideas in our Facebook Page.