| Talking Reminder this project is intenfed for people who are not tech savvy

AlarmReceiver.java
1 package com.sudogeeks.talking_reminder;
2
3 import android.app.AlarmManager;
4 import android.app.PendingIntent;
5 import android.content.BroadcastReceiver;
6 import android.content.ComponentName;
7 import android.content.Context;
8 import android.content.Intent;
9 import android.content.pm.PackageManager;
10 import android.os.SystemClock;
11
12 import androidx.core.app.NotificationCompat;
13
14 import java.util.Calendar;
15
21 public class AlarmReceiver extends BroadcastReceiver {
22 AlarmManager mAlarmManager;
23 PendingIntent mPendingIntent;
24
32 @Override
33 public void onReceive(Context context, Intent intent) {
34 int mReceivedID = Integer.parseInt(intent.getStringExtra(ReminderEditActivity.EXTRA_REMINDER_ID));
35
36 //Get notification title from ReminderDO Database
37 ReminderDatabase rb = new ReminderDatabase(context);
38 ReminderDO reminderDO = rb.getReminder(mReceivedID);
39 String mTitle = reminderDO.getTitle();
40
41
42 // Create intent to open ReminderEditActivity on notification click
43 Intent editIntent = new Intent(context, ReminderEditActivity.class);
44 editIntent.putExtra(ReminderEditActivity.EXTRA_REMINDER_ID, Integer.toString(mReceivedID));
45 PendingIntent mClick = PendingIntent.getActivity(context, mReceivedID, editIntent, PendingIntent.FLAG_UPDATE_CURRENT);
46
47
48 NotificationHelper notificationHelper = new NotificationHelper(context, intent);
49
50 NotificationCompat.Builder nb = notificationHelper.getChannelNotification();
51 notificationHelper.getManager().notify(mReceivedID, nb.build());
52
53 // tts
54 // context.startService(new Intent(context, ReadReminderTextService.class));
55
56 Intent i = new Intent(context, ReadReminderTextService.class);
57 i.putExtra("MyTitle", mTitle);
58 context.startService(i);
59 context.startService(i);
60 context.startService(i);
61
62
63 }
64
73 public void setAlarm(Context context, Calendar calendar, int ID) {
74 mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
75
76 // Put ReminderDO ID in Intent Extra
77 Intent intent = new Intent(context, AlarmReceiver.class);
78 intent.putExtra(ReminderEditActivity.EXTRA_REMINDER_ID, Integer.toString(ID));
79 mPendingIntent = PendingIntent.getBroadcast(context, ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
80
81 // Calculate notification time
82 Calendar c = Calendar.getInstance();
83 long currentTime = c.getTimeInMillis();
84 long diffTime = calendar.getTimeInMillis() - currentTime;
85
86 // Start alarm using notification time
87 mAlarmManager.set(AlarmManager.ELAPSED_REALTIME,
88 SystemClock.elapsedRealtime() + diffTime,
89 mPendingIntent);
90
91 // Restart alarm if device is rebooted
92 ComponentName receiver = new ComponentName(context, BootReceiver.class);
93 PackageManager pm = context.getPackageManager();
94 pm.setComponentEnabledSetting(receiver,
95 PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
96 PackageManager.DONT_KILL_APP);
97 }
98
107 public void setRepeatAlarm(Context context, Calendar calendar, int ID, long RepeatTime) {
108 mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
109
110 // Put ReminderDO ID in Intent Extra
111 Intent intent = new Intent(context, AlarmReceiver.class);
112 intent.putExtra(ReminderEditActivity.EXTRA_REMINDER_ID, Integer.toString(ID));
113 mPendingIntent = PendingIntent.getBroadcast(context, ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
114
115 // Calculate notification timein
116 Calendar c = Calendar.getInstance();
117 long currentTime = c.getTimeInMillis();
118 long diffTime = calendar.getTimeInMillis() - currentTime;
119
120 // Start alarm using initial notification time and repeat interval time
121 mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME,
122 SystemClock.elapsedRealtime() + diffTime,
123 RepeatTime, mPendingIntent);
124
125 // Restart alarm if device is rebooted
126 ComponentName receiver = new ComponentName(context, BootReceiver.class);
127 PackageManager pm = context.getPackageManager();
128 pm.setComponentEnabledSetting(receiver,
129 PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
130 PackageManager.DONT_KILL_APP);
131 }
132
139 public void cancelAlarm(Context context, int ID) {
140 mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
141
142 // Cancel Alarm using ReminderDO ID
143 mPendingIntent = PendingIntent.getBroadcast(context, ID, new Intent(context, AlarmReceiver.class), 0);
144 mAlarmManager.cancel(mPendingIntent);
145
146 // Disable alarm
147 ComponentName receiver = new ComponentName(context, BootReceiver.class);
148 PackageManager pm = context.getPackageManager();
149 pm.setComponentEnabledSetting(receiver,
150 PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
151 PackageManager.DONT_KILL_APP);
152 }
153 }
com.sudogeeks.talking_reminder.AlarmReceiver.setAlarm
void setAlarm(Context context, Calendar calendar, int ID)
Definition: AlarmReceiver.java:73
com.sudogeeks.talking_reminder.ReminderDatabase.getReminder
ReminderDO getReminder(int id)
Definition: ReminderDatabase.java:106
com.sudogeeks.talking_reminder.NotificationHelper.getChannelNotification
NotificationCompat.Builder getChannelNotification()
Definition: NotificationHelper.java:71
com.sudogeeks.talking_reminder.ReminderDO
Data Object class for Reminder This class conatins all the attributes of reminder.
Definition: ReminderDO.java:15
com.sudogeeks.talking_reminder.ReminderDatabase
Class to manage database This class is for managing the database.
Definition: ReminderDatabase.java:15
com.sudogeeks.talking_reminder.BootReceiver
A class For handling the reboot scenarios. In this case this class reschedules the alarms if the devi...
Definition: BootReceiver.java:15
com.sudogeeks.talking_reminder.ReadReminderTextService
Class for Text to speech service This class is extending the tts module service to speak up the remin...
Definition: ReadReminderTextService.java:13
com.sudogeeks.talking_reminder.AlarmReceiver
A class implementing basic alarm functions This class includes very basic features for an alarm.
Definition: AlarmReceiver.java:21
com.sudogeeks.talking_reminder.AlarmReceiver.onReceive
void onReceive(Context context, Intent intent)
Definition: AlarmReceiver.java:33
com.sudogeeks.talking_reminder.NotificationHelper
Class to implement notification This class is for implementing notification popup at alarm time.
Definition: NotificationHelper.java:20
com.sudogeeks.talking_reminder.AlarmReceiver.cancelAlarm
void cancelAlarm(Context context, int ID)
Definition: AlarmReceiver.java:139
com.sudogeeks.talking_reminder.ReminderEditActivity
Java class for handling UI component of Edit reminder layout.
Definition: ReminderEditActivity.java:32
com.sudogeeks.talking_reminder.AlarmReceiver.setRepeatAlarm
void setRepeatAlarm(Context context, Calendar calendar, int ID, long RepeatTime)
Definition: AlarmReceiver.java:107