Skip to content

Commit

Permalink
Alarm set when reminder is created
Browse files Browse the repository at this point in the history
  • Loading branch information
blanyal committed May 1, 2015
1 parent b1c32ff commit f28dea0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 25 deletions.
13 changes: 6 additions & 7 deletions app/src/main/java/com/blanyal/remindme/AlarmReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public void onReceive(Context context, Intent intent) {
.setContentTitle(context.getResources().getString(R.string.app_name))
.setContentText("This is a reminder")
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setPriority(NotificationCompat.PRIORITY_MAX)
.setOnlyAlertOnce(true);

int mNotificationId = 1;
Expand All @@ -64,10 +63,10 @@ public void onReceive(Context context, Intent intent) {

}

public void setAlarm(Context context, Calendar calendar)
public void setAlarm(Context context, Calendar calendar, int ID)
{
mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
mPendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, AlarmReceiver.class), 0);
mPendingIntent = PendingIntent.getBroadcast(context, ID, new Intent(context, AlarmReceiver.class), 0);

Calendar c = Calendar.getInstance();
long currentTime = c.getTimeInMillis();
Expand All @@ -76,10 +75,10 @@ public void setAlarm(Context context, Calendar calendar)
Log.d("TIME1:", Long.toString(calendar.getTimeInMillis()));
Log.d("TIME2:", Long.toString(currentTime));
Log.d("TIME3:", Long.toString(diffTime));
Log.d("ID:", Integer.toString(ID));

mAlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
mAlarmManager.set(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + diffTime,
diffTime,
mPendingIntent);

// Restart alarm if device is rebooted
Expand All @@ -90,10 +89,10 @@ public void setAlarm(Context context, Calendar calendar)
PackageManager.DONT_KILL_APP);
}

public void cancelAlarm(Context context)
public void cancelAlarm(Context context, int ID)
{
mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
mPendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, AlarmReceiver.class), 0);
mPendingIntent = PendingIntent.getBroadcast(context, ID, new Intent(context, AlarmReceiver.class), 0);
mAlarmManager.cancel(mPendingIntent);

// Disable BootReceiver so that alarm won't start again if device is rebooted
Expand Down
17 changes: 8 additions & 9 deletions app/src/main/java/com/blanyal/remindme/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,16 @@ public void onClick(View v) {
}
});

mCalendar = Calendar.getInstance();
// mCalendar = Calendar.getInstance();

mCalendar.set(Calendar.MONTH, 3);
mCalendar.set(Calendar.YEAR, 2015);
mCalendar.set(Calendar.DAY_OF_MONTH, 29);
mCalendar.set(Calendar.HOUR_OF_DAY, 11);
mCalendar.set(Calendar.MINUTE, 3);
mCalendar.set(Calendar.SECOND, 0);
// mCalendar.set(Calendar.MONTH, 3);
// mCalendar.set(Calendar.YEAR, 2015);
// mCalendar.set(Calendar.DAY_OF_MONTH, 29);
// mCalendar.set(Calendar.HOUR_OF_DAY, 11);
// mCalendar.set(Calendar.MINUTE, mCalendar.get(Calendar.MINUTE) + 1);
// mCalendar.set(Calendar.SECOND, 0);


new AlarmReceiver().setAlarm(getApplicationContext(), mCalendar);
// new AlarmReceiver().setAlarm(getApplicationContext(), mCalendar, 1);
}

@Override
Expand Down
19 changes: 18 additions & 1 deletion app/src/main/java/com/blanyal/remindme/ReminderAddActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,18 @@ public void setDate(View v){

@Override
public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
mTime = hourOfDay + ":" + minute;
mTimeText.setText(mTime);
}

@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
monthOfYear ++;
mDay = dayOfMonth;
mMonth = monthOfYear;
mYear = year;
mDate = dayOfMonth + "/" + monthOfYear + "/" + year;
mDateText.setText(mDate);
}
Expand Down Expand Up @@ -293,7 +298,7 @@ public void saveReminder(){
ReminderDatabase rb = new ReminderDatabase(this);

// Inserting Reminder
rb.addReminder(new Reminder(mTitle, mDate, mTime, mRepeat, mRepeatNo, mRepeatType, mActive));
int ID = rb.addReminder(new Reminder(mTitle, mDate, mTime, mRepeat, mRepeatNo, mRepeatType, mActive));

List<Reminder> reminders = rb.getAllReminders();

Expand All @@ -305,6 +310,18 @@ public void saveReminder(){
Log.d("Name: ", log);
}

Log.d("DETAILS:", "Month: " + mMonth + " Year: " + mYear + " Day: " + mDay
+ " Hour: " + mHour + " Minute: " + mMinute);

mCalendar.set(Calendar.MONTH, -- mMonth);
mCalendar.set(Calendar.YEAR, mYear);
mCalendar.set(Calendar.DAY_OF_MONTH, mDay);
mCalendar.set(Calendar.HOUR_OF_DAY, mHour);
mCalendar.set(Calendar.MINUTE, mMinute);
mCalendar.set(Calendar.SECOND, 0);

new AlarmReceiver().setAlarm(getApplicationContext(), mCalendar, ID);

Toast.makeText(getApplicationContext(), "Saved",
Toast.LENGTH_SHORT).show();

Expand Down
12 changes: 5 additions & 7 deletions app/src/main/java/com/blanyal/remindme/ReminderDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@
*/




package com.blanyal.remindme;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
Expand All @@ -29,6 +26,7 @@
import java.util.ArrayList;
import java.util.List;


public class ReminderDatabase extends SQLiteOpenHelper {

// Database Version
Expand Down Expand Up @@ -83,7 +81,7 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}

// Adding new Reminder
public void addReminder(Reminder reminder){
public int addReminder(Reminder reminder){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();

Expand All @@ -96,17 +94,17 @@ public void addReminder(Reminder reminder){
values.put(KEY_ACTIVE, reminder.getActive());

// Inserting Row
db.insert(TABLE_REMINDERS, null, values);
long ID = db.insert(TABLE_REMINDERS, null, values);
db.close();

return (int) ID;
}


// Getting single Reminder
public Reminder getReminder(int id){
SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.query(TABLE_REMINDERS,new String[]
Cursor cursor = db.query(TABLE_REMINDERS, new String[]
{
KEY_ID,
KEY_TITLE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void onReceive(Context context, Intent intent) {
mCalendar.set(Calendar.SECOND, 0);
mCalendar.set(Calendar.AM_PM, Calendar.PM);

new AlarmReceiver().setAlarm(context, mCalendar);
new AlarmReceiver().setAlarm(context, mCalendar, 1);

// Delete the notification
NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Expand Down

0 comments on commit f28dea0

Please sign in to comment.