From f28dea05c598062954f11ffdc1c5190a6a283f86 Mon Sep 17 00:00:00 2001 From: blanyal Date: Fri, 1 May 2015 21:51:15 +0530 Subject: [PATCH] Alarm set when reminder is created --- .../com/blanyal/remindme/AlarmReceiver.java | 13 ++++++------- .../com/blanyal/remindme/MainActivity.java | 17 ++++++++--------- .../blanyal/remindme/ReminderAddActivity.java | 19 ++++++++++++++++++- .../blanyal/remindme/ReminderDatabase.java | 12 +++++------- .../blanyal/remindme/ReminderReceiver.java | 2 +- 5 files changed, 38 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/com/blanyal/remindme/AlarmReceiver.java b/app/src/main/java/com/blanyal/remindme/AlarmReceiver.java index 8bb0dc4..34a55bb 100644 --- a/app/src/main/java/com/blanyal/remindme/AlarmReceiver.java +++ b/app/src/main/java/com/blanyal/remindme/AlarmReceiver.java @@ -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; @@ -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(); @@ -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 @@ -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 diff --git a/app/src/main/java/com/blanyal/remindme/MainActivity.java b/app/src/main/java/com/blanyal/remindme/MainActivity.java index 218acd4..6b14f59 100644 --- a/app/src/main/java/com/blanyal/remindme/MainActivity.java +++ b/app/src/main/java/com/blanyal/remindme/MainActivity.java @@ -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 diff --git a/app/src/main/java/com/blanyal/remindme/ReminderAddActivity.java b/app/src/main/java/com/blanyal/remindme/ReminderAddActivity.java index 1dbd00d..9c59f9e 100644 --- a/app/src/main/java/com/blanyal/remindme/ReminderAddActivity.java +++ b/app/src/main/java/com/blanyal/remindme/ReminderAddActivity.java @@ -203,6 +203,8 @@ 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); } @@ -210,6 +212,9 @@ public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) { @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); } @@ -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 reminders = rb.getAllReminders(); @@ -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(); diff --git a/app/src/main/java/com/blanyal/remindme/ReminderDatabase.java b/app/src/main/java/com/blanyal/remindme/ReminderDatabase.java index 4c0540d..7e35649 100644 --- a/app/src/main/java/com/blanyal/remindme/ReminderDatabase.java +++ b/app/src/main/java/com/blanyal/remindme/ReminderDatabase.java @@ -15,11 +15,8 @@ */ - - package com.blanyal.remindme; - import android.content.ContentValues; import android.content.Context; import android.database.Cursor; @@ -29,6 +26,7 @@ import java.util.ArrayList; import java.util.List; + public class ReminderDatabase extends SQLiteOpenHelper { // Database Version @@ -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(); @@ -96,9 +94,9 @@ 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; } @@ -106,7 +104,7 @@ public void addReminder(Reminder 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, diff --git a/app/src/main/java/com/blanyal/remindme/ReminderReceiver.java b/app/src/main/java/com/blanyal/remindme/ReminderReceiver.java index 2fe4f4a..3fbcd9c 100644 --- a/app/src/main/java/com/blanyal/remindme/ReminderReceiver.java +++ b/app/src/main/java/com/blanyal/remindme/ReminderReceiver.java @@ -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);