基于Android的数据库值/时间设置闹钟?

问题描述:

我有一个数据库设置了3列,ID,TASK和TIME。时间从Timepicker放置在数据库中。我需要弄清楚如何读取数据库中的时间值,然后根据这些时间设置闹钟,任何想法?基于Android的数据库值/时间设置闹钟?

我的按钮有以下代码来设置报警并将值添加到数据库中。

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); 

我报警接收机类有以下几点:

@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()); 

} 

因此我需要弄清楚(在您的帮助:)) 如何设置多个闹钟 如何把它们发给基础上,数据库值 我需要它们每天重复同时进行。 setRepeating我认为。

+1

由于您在1.5小时内没有得到答案,因此不会回答问题。 – Soana

为了从数据库读取值,请使用游标。

登录下面的代码打印数据库中的值。

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(); 
    }