0% found this document useful (0 votes)
44 views8 pages

MADEX02

Uploaded by

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

MADEX02

Uploaded by

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

61781922106041

EX NO : 2
DATE PICKER AND ALARM MANAGER
DATE: 12/08/24

AIM:
To develop an application to manage date picker and alarm using android studio.

PROCEDURE:
Step 1: Open Android Studio and create a new project with an empty activity
template.
Step 2: Add permissions in `AndroidManifest.xml` for setting alarms.
Step 3: Modify `activity_main.xml` to include a `DatePicker`, `TimePicker`, and a
`Button` to set the alarm.
Step 4: Update `build.gradle` if necessary for alarm-related dependencies.
Step 5: In `MainActivity.java`, set up variables for `DatePicker`, `TimePicker`, and
the `Button`.
Step 6: Implement the logic to get the selected date and time from the pickers and
schedule an alarm using `AlarmManager`.
Step 7: Add code to display a toast notification when the alarm is successfully set.
Step 8: Test the application on a device to verify date picking and alarm functionality.
PROGRAM:

MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- TextView to display the selected time -->
61781922106041

<TextView
android:id="@+id/textViewSelectedTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selected Time: Not Set"
android:textSize="18sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="20dp" />
<!-- Button to open the time picker dialog -->
<Button
android:id="@+id/buttonSelectTime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Select Time"
app:layout_constraintTop_toBottomOf="@id/textViewSelectedTime"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="20dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp" />
<!-- Button to set the alarm -->
<Button
android:id="@+id/buttonSetAlarm"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Set Alarm"
app:layout_constraintTop_toBottomOf="@id/buttonSelectTime"
app:layout_constraintStart_toStartOf="parent"
61781922106041

app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="20dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp" />
<!-- Button to cancel the alarm -->
<Button
android:id="@+id/buttonCancelAlarm"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Cancel Alarm"
app:layout_constraintTop_toBottomOf="@id/buttonSetAlarm"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="20dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.alarmmanagement;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlarmManager;
import android.app. NotificationChannel;
import android.app. NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
61781922106041

import android.view.View;
import android.widget.Toast;
import com.example.alarmmanagement.databinding. Activity MainBinding:
import com.google.android.material.timepicker.MaterialTimePicker;
import com.google.android.material.timepicker.TimeFormat;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
private MaterialTimePicker picker;
private Calendar calendar;
private AlarmManager alarmManager;
private PendingIntent pendingintent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
createNotificationChannel();
binding.selectTimeBtn.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
showTimePicker();
});
binding.setAlarmBtn.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
setAlarm();}
});
binding.cancelAlarmBtn.setOnClickListener(new View.OnClickListener() (
@Override
public void onClick(View v) {
cancelAlarm();
61781922106041

}});}
private void cancelAlarm() {
Intent intent = new Intent(this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this,0, intent,0);
if (alarmManager == null) {
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingintent);
Toast.makeText(this, "Alarm Cancelled", Toast.LENGTH_SHORT).show();}
private void setAlarm() {
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, Alarm Receiver.class);
pendingIntent = PendingIntent.getBroadcast(this,0, intent,0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY,pendingIntent);
Toast.makeText(this, "Alarm set Successfully", Toast.LENGTH_SHORT).show();
private void showTimePicker() {
picker = new MaterialTimePicker.Builder()
setTimeFormat(TimeFormat.CLOCK_12H)
.setHour(12)
.setMinute(0)
setTitleText("Select Alarm Time")
.build();
picker.show(getSupportFragmentManager(), "foxandroid");
picker.addOnPositiveButtonClickListener(new View.OnClickListener() (
@Override
public void onClick(View v) {
if (picker.getHour() > 12){
binding.selectedTime.setText( String.format("%02d", (picker.getHour()-12))+":
"+String.format("%02d",
picker.getMinute())+" PM"
);
61781922106041

} else {
binding.selectedTime.setText(picker.getHour()+":"+ picker.getMinute() + "AM");
calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,picker.getHour());
calendar.set(Calendar.MINUTE, picker.getMinute());
calendar.set(Calendar. SECOND,0);
calendar.set(Calendar.MILLISECOND,0); });
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
CharSequence name = "foxandroid ReminderChannel";
String description = "Channel For Alarm Manager";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel("foxandroid", name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel (channel);
}
}}

AlarmReceiver.java
package com.example.alarmmanagement; //need to change
import android.app.NotificationManager;

import android.app. PendingIntent;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import androidx.core.app.NotificationCompat;

import androidx.core.app.NotificationManagerCompat;

public class AlarmReceiver extends BroadcastReceiver ( @Override

public void onReceive(Context context, Intent intent) {


61781922106041

Intent i= new Intent(context, DestinationActivity.class);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

PendingIntent pendingIntent = PendingIntent.getActivity(context,0,1,0);

NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "foxandroid")

.setSmallicon(R.drawable.ic_launcher_background)

setContentTitle("Foxandroid Alarm Manager")

.setContentText("Subscribe for android related content")

.setAutoCancel(true)

setDefaults(NotificationCompat.DEFAULT_ALL)

setPriority(NotificationCompat. PRIORITY_HIGH) setContentIntent(pendingIntent);

NotificationManagerCompat notification ManagerCompat = Notification


ManagerCompat.from(context);

notificationManagerCompat.notify(123,builder.build());}

DestinationActivity.java
package com.example.alarmmanagement; //need to change

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class Destination Activity extends AppCompatActivity {


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_destination);
}
}
61781922106041

OUTPUT:

RESULT:
Thus, the program to manage date picker and alarm manager has been created
successfully using android studio.

You might also like