2016-03-07 31 views
0

我有一個數據庫設置了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

回答

0

爲了從數據庫讀取值,請使用遊標。

登錄下面的代碼打印數據庫中的值。

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