简体   繁体   中英

Set an alarm based of a Database value/time for Android?

I have a database set up with 3 Columns, ID, TASK and TIME. The time is placed in the DB from a Timepicker. I need to figure out how to read the Time values in the database then set the alarm based of these times, any ideas?

My button has the following code to set Alarm and add values into db.

final EditText edt = (EditText) dialogView.findViewById(R.id.edit1);
                    tp = (TimePicker) dialogView.findViewById(R.id.timePicker);


                    String task = edt.getText().toString(); //retrieve vales from fields

                    String strDateTime = tp.getCurrentHour() + ":" + tp.getCurrentMinute();
                    int minutes = tp.getCurrentMinute();
                    int hourr = tp.getCurrentHour();

                    Intent alarmIntent = new Intent(Meds2.this, AlarmReceiver.class);
                    pendingIntent = PendingIntent.getBroadcast(Meds2.this, 0, alarmIntent, 0);


                    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                    int interval = 1000 * 60 * 20;

                     /* Set the alarm to start at 10:30 AM */
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(System.currentTimeMillis());
                    calendar.set(Calendar.HOUR_OF_DAY, hourr);
                    calendar.set(Calendar.MINUTE, minutes);

                    Log.e(TAG, "Time is set at: " + calendar.getTime());

                    /* Repeating on every 20 minutes interval */
                    manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                            1000 * 60 * 20, pendingIntent);



                    TaskDBHelper helper = new TaskDBHelper(Meds2.this);
                    SQLiteDatabase sqlDB = helper.getWritableDatabase();
                    ContentValues values = new ContentValues();

                    values.clear();

                    values.put(TaskContract.Columns.TASK, task); //place values in db
                    values.put(TaskContract.Columns.DATE, strDateTime);


                    sqlDB.insertWithOnConflict(TaskContract.TABLE, null, values,
                            SQLiteDatabase.CONFLICT_IGNORE);

My Alarm Receiver Class has the following:

 @Override
public void onReceive(Context context, Intent intent)
{
    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_cast_light)
                    .setContentTitle("Please Take Your Meds")
                    .setContentInfo("Time")
                    .setContentText("Enjoy" )
                    .setDefaults(Notification.DEFAULT_SOUND)
                    .setAutoCancel(true)
                    .setVibrate(new long[]{1000, 1000, 1000, 1000}) //set virbrate & color of led
                    .setLights(Color.BLUE, 3000, 3000);

    int NOTIFICATION_ID = 12345;

    NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    nManager.notify(NOTIFICATION_ID, builder.build());

}

Therefore I need to figure out(with your help:)) How to set multiple Alarms How to then set them based on the Database values I need them to repeat everyday at the same time. setRepeating I think.

In order to read values from Database use the cursor.

The Log in the code below prints the values from the db.

Cursor cursor = sqlDB.query(TaskContract.TABLE,
            new String[]{TaskContract.Columns._ID, TaskContract.Columns.TASK, TaskContract.Columns.DATE},
            null, null, null, null, null);

    cursor.moveToFirst();

    while (!cursor.isAfterLast()) { // Loop until all vales have been seen

        String time = cursor.getString(2);
        String[] parts = time.split(":"); //Split  String Value stored in db
        String part1 = parts[0]; // hour
        String part2 = parts[1]; // minute
        int hr = Integer.parseInt(part1);
        int min = Integer.parseInt(part2);

        final int _id=(int)System.currentTimeMillis();

        Intent alarmIntent = new Intent(Meds2.this, AlarmReceiver.class); //set up alarm
        pendingIntent = PendingIntent.getBroadcast(Meds2.this, _id, alarmIntent, 0);

        AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, hr);
        calendar.set(Calendar.MINUTE, min); //set cal time based of db value

                    /* Repeating on every 24 hours interval */
        manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, pendingIntent); // run every date.time() in millisec

        Log.e(TAG, "DATE VALUE TIME IS " + part1 +"--" +part2); //log part one and two to make sure time is right

        cursor.moveToNext();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM