0% found this document useful (0 votes)
142 views6 pages

Practical-10: AIM:-Write A Mobile Application That Creates Alarm Clock. Activity - Main - XML

This document describes how to create an alarm clock mobile application using Android. It includes the XML layout code to include a time picker and toggle button. The Java code shows how to set the alarm using the AlarmManager when the toggle button is turned on, and cancel the alarm when turned off. It also includes a BroadcastReceiver class that plays a ringtone when the alarm goes off. The application allows setting the time for the alarm and turning it on/off.

Uploaded by

devv
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)
142 views6 pages

Practical-10: AIM:-Write A Mobile Application That Creates Alarm Clock. Activity - Main - XML

This document describes how to create an alarm clock mobile application using Android. It includes the XML layout code to include a time picker and toggle button. The Java code shows how to set the alarm using the AlarmManager when the toggle button is turned on, and cancel the alarm when turned off. It also includes a BroadcastReceiver class that plays a ringtone when the alarm goes off. The application allows setting the time for the alarm and turning it on/off.

Uploaded by

devv
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/ 6

Er no:-170160116062

PRACTICAL–10
AIM:-Write a mobile application that creates alarm clock.
activity_main.xml:-
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TimePicker
android:id="@+id/timepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
</TimePicker>
<ToggleButton
android:id="@+id/togglebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Er no:-170160116062

android:layout_gravity="center"
android:layout_margin="20dp"
android:checked="false"
android:onClick="OnToggleClicked"
tools:gnore="OnClick"/>
</LinearLayout>
</LinearLayout>
MainActivity.java:-
packagecom.example.alarm;
importandroidx.appcompat.app.AppCompatActivity;
importandroid.app.AlarmManager;
importandroid.app.PendingIntent;
importandroid.content.Intent;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.TimePicker;
importandroid.widget.Toast;
importandroid.widget.ToggleButton;
importjava.util.Calendar;
publicclassMainActivityextendsAppCompatActivity{
TimePickertimePicker;
PendingIntentpendingIntent;
AlarmManageralarmManager;
@Override
protectedvoidonCreate(BundlesavedInstanceState)
{super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timePicker=findViewById(R.id.timepicker);
Er no:-170160116062

alarmManager=(AlarmManager)getSystemService(ALARM_SERVICE);
}
publicvoidOnToggleClicked(Viewview)
{
longtime;
if(((ToggleButton)view).isChecked())
{

Toast.makeText(MainActivity.this,"ALARMON",Toast.LENGTH_SHORT).sh
ow();
Calendarcalendar=Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE,timePicker.getCurrentMinute());
Intentintent=newIntent(this,AlarmReceiver.class);pendingIntent=
PendingIntent.getBroadcast(this,0,intent,0);
time=(calendar.getTimeInMillis()-(calendar.getTimeInMillis()%60000));if
(System.currentTimeMillis()>time){
if(calendar.AM_PM==0)
time=time+(1000*60*60*12);
else
time=time+(1000*60*60*24);
}
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,time,10000,
pendingIntent);
}
else{
alarmManager.cancel(pendingIntent);
Er no:-170160116062

Toast.makeText(MainActivity.this,"ALARMOFF",Toast.LENGTH_SHORT).s
how();
}
}
}
AlarmReceiver.java:-
packagecom.example.alarm;
importandroid.content.BroadcastReceiver;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.media.Ringtone;
importandroid.media.RingtoneManager;
importandroid.net.Uri;
importandroid.widget.Toast;
publicclassAlarmReceiverextendsBroadcastReceiver{
@Override
publicvoidonReceive(Contextcontext,Intentintent){
Toast.makeText(context,"Alarm!Wakeup!
Wakeup!",Toast.LENGTH_SHORT).show();
UrialarmUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALAR
M);
if(alarmUri==null)
{
alarmUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFIC
ATION);
}
Ringtoneringtone=RingtoneManager.getRingtone(context,alarmUri);ringtone.pl
ay();
}
Er no:-170160116062

}
OUTPUT:-
Er no:-170160116062

You might also like