0% found this document useful (0 votes)
125 views5 pages

Alaram

This document provides steps to create a scheduled task example using the Android AlarmManager. It involves creating a new project, building a simple UI with a timepicker and button, creating a broadcast receiver class to fire the alarm, registering the receiver in the manifest, and setting up the alarm in the main activity class using the timepicker value.

Uploaded by

Jaini Kavya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views5 pages

Alaram

This document provides steps to create a scheduled task example using the Android AlarmManager. It involves creating a new project, building a simple UI with a timepicker and button, creating a broadcast receiver class to fire the alarm, registering the receiver in the manifest, and setting up the alarm in the main activity class using the timepicker value.

Uploaded by

Jaini Kavya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Android Scheduled Task Example using AlarmManager

Creating a new Project

 As always we will be creating a new Android Studio Project. So here I


am with my project named AlarmManagerExample.

Building UI

Inside activity_main.xml we will create the following UI. This is pretty


simple we have only a TimePicker and a Button.

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context="net.simplifiedlearning.alarmmanagerexample.MainActi
vity">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical">

<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<Button
android:id="@+id/buttonAlarm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Set Alarm" />

</LinearLayout>

</RelativeLayout>
Creating a Broadcast Receiver for the Alarm

 We need a Broadcast Receiver to fire the alarm when our app is not running.

Creating BroadcastReceiver

So create a java class named MyAlarm.java and write the following code.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

//class extending the Broadcast Receiver


public class MyAlarm extends BroadcastReceiver {

//the method will be fired when the alarm is triggerred


@Override
public void onReceive(Context context, Intent intent) {

//you can check the log that it is fired


//Here we are actually not doing anything
//but you can do any task here that you want to be done at a specific
time everyday
Log.d("MyAlarmBelal", "Alarm just fired");
}

Registering BroadcastReceiver

We also need to register this BroadcastReceiver in the Manifest file. So


here is the AndroidManifest.xml file where you need to register your
receiver just before the </application> tag.

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.simplifiedlearning.alarmmanagerexample">
<uses-permission android:name="android.permission.WAKE_LOCK"
/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<!-- registering the receiver -->


<receiver
android:name=".MyAlarm"
android:enabled="true"
android:exported="true" />
</application>

</manifest>

Setting the Alarm

Now the final step is setting up the Alarm. We will do this inside MainActivity.java.

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

//the timepicker object


TimePicker timePicker;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//getting the timepicker object


timePicker = (TimePicker) findViewById(R.id.timePicker);

//attaching clicklistener on button


findViewById(R.id.buttonAlarm).setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
//We need a calendar object to get the specified time in millis
//as the alarm manager method takes time in millis to setup the alarm
Calendar calendar = Calendar.getInstance();
if (android.os.Build.VERSION.SDK_INT >= 23) {
calendar.set(calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),
timePicker.getHour(), timePicker.getMinute(), 0);
} else {
calendar.set(calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),
timePicker.getCurrentHour(), timePicker.getCurrentMinute(),
0);
}

setAlarm(calendar.getTimeInMillis());
}
});
}

private void setAlarm(long time) {


//getting the alarm manager
AlarmManager am = (AlarmManager)
getSystemService(Context.ALARM_SERVICE);

//creating a new intent specifying the broadcast receiver


Intent i = new Intent(this, MyAlarm.class);

//creating a pending intent using the intent


PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);

//setting the repeating alarm that will be fired every day


am.setRepeating(AlarmManager.RTC, time,
AlarmManager.INTERVAL_DAY, pi);
Toast.makeText(this, "Alarm is set", Toast.LENGTH_SHORT).show();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy