如何在Android的提醒应用中编写计时器?

时间:2016-08-03 12:10:24

标签: android timer reminders

我是一名学习android编程的学生。目前,我正在制作一个提醒应用程序,允许用户在某一天使用时间选择器输入3次。在这些时候,他会被提醒一些事情。我将这些数据存储在数据库中。用户还选择希望被提醒的日期而不是日期。我的问题是如何在服务中设置计时器,可以检索数据检查是否在特定日期,然后为此发出警报。以下是我尝试过的代码。它有效,但我想编写一个更有效的程序。我对我的编码不满意。

以下是我的代码:

MY DATABASE CLASS

//this is my database
String reminderCreateQuery = "CREATE TABLE reminders" +
                    "(_id integer primary key autoincrement," +
                    "message TEXT" +
                    "hour1 INTEGER, min1 INTEGER, hour2 INTEGER, min2 INTEGER, hour3 INTEGER, min3 INTEGER," +
                    "AllDays INTEGER, Sun INTEGER, Mon INTEGER, Tue INTEGER, Wed INTEGER, Thu INTEGER, Fri INTEGER, Sat INTEGER);";

//this is where I am inserting my data in the database
public void insertReminder(String message, int hour1, int min1, int hour2,
                                   int min2, int hour3, int min3, int AllDays, int Sun, int Mon, int Tue, int Wed,
                                   int Thu, int Fri, int Sat) {
        ContentValues newReminder = new ContentValues();
        newReminder.put("message", message);
        newReminder.put("hour1", hour1);
        newReminder.put("min1", min1);
        newReminder.put("hour2", hour2);
        newReminder.put("min2", min2);
        newReminder.put("hour3", hour3);
        newReminder.put("min3", min3);
        newReminder.put("Sun", Sun);
        newReminder.put("Mon", Mon);
        newReminder.put("Tue", Tue);
        newReminder.put("Wed", Wed);
        newReminder.put("Thu", Thu);
        newReminder.put("Fri", Fri);
        newReminder.put("Sat", Sat);
        newReminder.put("AllDays", AllDays);
        open();
        reminderdatabase.insert("reminders", null, newReminder);
        close();
    }

//this is where i am updating my data in the database

public void updateReminder(long id, String message,int hour1, int min1, int hour2,
                                   int min2, int hour3, int min3, int AllDays, int Sun, int Mon, int Tue, int Wed,
                                   int Thu, int Fri, int Sat) {
        ContentValues editReminder = new ContentValues();
        editReminder.put("message", message);
        editReminder.put("hour1", hour1);
        editReminder.put("min1", min1);
        editReminder.put("hour2", hour2);
        editReminder.put("min2", min2);
        editReminder.put("hour3", hour3);
        editReminder.put("min3", min3);
        editReminder.put("Sun", Sun);
        editReminder.put("Mon", Mon);
        editReminder.put("Tue", Tue);
        editReminder.put("Wed", Wed);
        editReminder.put("Thu", Thu);
        editReminder.put("Fri", Fri);
        editReminder.put("Sat", Sat);
        editReminder.put("AllDays", AllDays);

        open();
        reminderdatabase.update("reminders", editReminder, "_id=" + id, null);
        close();
    }

    //this gets called in AsynClass where it returns the cursor
public Cursor getSundayReminders() {

        return remindersdatabase.query("reminders", null, "Sun = ?", new String[] {"1"}, null, null,null);
    }

MY ACTIVITY CLASS

//this is where i am saving or editing my data in the database

private void saveReminder() {
        DatabaseConnector databaseConnector = new DatabaseConnector(this);
        if (getIntent().getExtras() == null) {
            databaseConnector.insertReminder(
                    MessageNameLabe.getText().toString(),
                    hour1, minute1, hour2, minute2, hour3, minute3,
                    Sun, Mon, Tue, Wed, Thu, Fri, Sat);

        } else {
            databaseConnector.updateReminder(rowID,
                    MessageNameLabe.getText().toString(),
                    hour1, minute1, hour2, minute2, hour3, minute3,
                    Sun, Mon, Tue, Wed, Thu, Fri, Sat);
        }
        startMyAlarm();
    }

    public void startMyAlarm() {
        Intent intent = new Intent(MyReminderAddActivity.this, SetMyAlarm.class);
        startService(intent);
    }

我的服务类

// I have a timer that calls a function getmyDays() in my service..This function gets called in saveReminder() function

public void startTimer() {
        TimerTask task = new TimerTask() {

            @Override
            public void run() {
                checkMyDays();
            }
        };
        timer = new Timer(true);
        timer.schedule(task, 0, 3600000);
    }

//checkMyDays() checks the day for today...I have only put the example for Sunday
public void checkMyDays() {
        if (checkdays.isTodaySunday()) {
            todayIsSunday = true;
            todayIsMonday = todayIsTuesday = todayIsWednesday = todayIsThrusday = todayIsFriday = todayIsSaturday = false;
        }
         getMyDays();
    }

//isTodaySunday() is just a function that checks todays day...I have only put the example for Sunday
public boolean isTodaySunday(){
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY;
    }


//my database function where sunday is 1
 public void getMyDays() {
        if (todayIsSunday) {
            new GetReminderSundayTask().execute();
        }
    }


//here i am getting data from the database

private class GetReminderSundayTask extends AsyncTask<Long, Object, Cursor> {
        DatabaseConnector prescriptiondatabaseConnector = new DatabaseConnector(SetMyAlarm.this);

        @Override
        protected Cursor doInBackground(Long... params) {
            remindersdatabaseConnector.open();

            return remindersdatabaseConnector.getSundayReminders();
        }

        @Override
        protected void onPostExecute(Cursor result) {
            super.onPostExecute(result);

            boolean data = result.moveToFirst();
            while (data) {
                int hour1Index = result.getColumnIndex("hour1");
                Hour1 = result.getInt(hour1Index);

                int min1Index = result.getColumnIndex("min1");
                Minute1 = result.getInt(min1Index);

                int hour2Index = result.getColumnIndex("hour2");
                Hour2 = result.getInt(hour2Index);

                int min2Index = result.getColumnIndex("min2");
                Minute2 = result.getInt(min2Index);

                int hour3Index = result.getColumnIndex("hour3");
                Hour3 = result.getInt(hour3Index);

                int min3Index = result.getColumnIndex("min3");
                Minute3 = result.getInt(min3Index);

                getMyMillis(Hour1, Minute1, Hour2, Minute2, Hour3, Minute3);

                if (timeInMilliSeconds1 > 0) {
                    Time1List.add(timeInMilliSecondsDosage1);
                    countTime1++;
                }
                if (timeInMilliSeconds2 > 0) {
                    Time2List.add(timeInMilliSecondsDosage2);
                    countForTime2++;
                }
                if (timeInMilliSeconds3 > 0) {
                    Time3List.add(timeInMilliSecondsDosage3);
                    countForTime3++;
                }
                int messageIndex = result.getColumnIndex("message");
                messageList.add(result.getString(messageIndex));
                count++;
                countIDList.add(count);
                data = result.moveToNext();
            }
            Log.d("My Alarm", "Count is " + count);
            result.close();
            remindersdatabaseConnector.close();
            startMyAlarm();
        }
    }


    // here I am converting my hour and min to millis
    public void getMyMillis(int hour1, int minute1, int hour2, int minute2, int hour3, int minute3) {
        if (hour1 != 0 && minute1 != 0) {
            CalendarTime1.set(Calendar.HOUR_OF_DAY, hour1);
            CalendarTime1.set(Calendar.MINUTE, minute1);
            timeInMilliSeconds1 = CalendarTime1.getTimeInMillis();
        } else {
            timeInMilliSeconds1 = 0;
        }
        if (hour2 != 0 && minute2 != 0) {
            CalendarTime2.set(Calendar.HOUR_OF_DAY, hour2);
            CalendarTime2.set(Calendar.MINUTE, minute2);
            timeInMilliSeconds2 = CalendarTime2.getTimeInMillis();
        } else {
            timeInMilliSeconds2 = 0;
        }

        if (hour3 != 0 && minute3 != 0) {
            CalendarTime3.set(Calendar.HOUR_OF_DAY, hour3);
            CalendarTime3.set(Calendar.MINUTE, minute3);
            timeInMilliSeconds3 = CalendarTime3.getTimeInMillis();
        } else {
            timeInMilliSeconds3 = 0;
        }
    }


//then in starMyAlarm() I fire my alarms
public void startMyAlarm() {
        Intent intent = new Intent(this, AlarmReceiver.class);
        if (countForTime1 > 0) {
            AlarmManager[] alarmMgrForTime1 = new AlarmManager[countForTime1];
            for (int i = 0; i < countForTime1.size(); i++) {
                if (Time1List != null) {
                    if (i < Time1List.size()) {
                        timeInMilliSeconds1 = Time1List.get(i);
                        if (timeInMilliSeconds1 > 0) {
                            firstTimedentifierList.add(firstTimeIdentifier);
                            intent.putExtra("TimeIdentifier", 1);
                            alarmIntentForTime1 = PendingIntent.getBroadcast(this, alarmRequestCodeForTime1, intent, 0);
                            alarmMgrForTime1[i] = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                            alarmMgrForTime1[i].set(AlarmManager.RTC_WAKEUP, timeInMilliSeconds1, alarmIntentForTime1);
                            alarmRequestCodeListForTime1.add(alarmRequestCodeForTime1);
                            intentArrayListForTime1.add(alarmIntentForTime1);
                            alarmRequestCodeForTime1++;
                            firstTimeIdentifier++;
                        }
                    }
                }
            }
        }
        if (countForTime2 > 0) {
            AlarmManager[] alarmMgrForTime2 = new AlarmManager[countForTime2];
            for (int i = 0; i < countForTime2.size(); i++) {
                if (Time2List != null) {
                    if (i < Time2List.size()) {
                        timeInMilliSeconds2 = Time2List.get(i);
                        if (timeInMilliSeconds2 > 0) {
                            secondTimedentifierList.add(secondTimeIdentifier);
                            intent.putExtra("TimeIdentifier", 2);
                            alarmIntentForTime2 = PendingIntent.getBroadcast(this, alarmRequestCodeForTime2, intent, 0);
                            alarmMgrForTime2[i] = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                            alarmMgrForTime2[i].set(AlarmManager.RTC_WAKEUP, timeInMilliSeconds2, alarmIntentForTime2);
                            alarmRequestCodeListForTime2.add(alarmRequestCodeForTime2);
                            intentArrayListForTime2.add(alarmIntentForTime2);
                            alarmRequestCodeForTime2++;
                            secondTimeIdentifier++;
                        }
                    }
                }
            }
        }
        if (countForTime3> 0) {
            AlarmManager[] alarmMgrForTime3 = new AlarmManager[countForTime3];
            for (int i = 0; i < countForTime3.size(); i++) {
                if (Time3List != null) {
                    if (i < Time3List.size()) {
                        timeInMilliSeconds3 = Time3List.get(i);
                        if (timeInMilliSeconds3 > 0) {
                            thirdTimedentifierList.add(thirdTimeIdentifier);
                            intent.putExtra("TimeIdentifier", 3);
                            alarmIntentForTime3 = PendingIntent.getBroadcast(this, alarmRequestCodeForTime3, intent, 0);
                            alarmMgrForTime1[i] = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                            alarmMgrForTime1[i].set(AlarmManager.RTC_WAKEUP, timeInMilliSeconds3, alarmIntentForTime3);
                            alarmRequestCodeListForTime3.add(alarmRequestCodeForTime3);
                            intentArrayListForTime3.add(alarmIntentForTime3);
                            alarmRequestCodeForTime3++;
                            thirdTimeIdentifier++;
                        }
                    }
                }
            }
        }
    }
}

有更好的方法吗?我应该有两个表用于数据,一个用于报警。从数据库中删除数据后如何取消特定警报?我已经保存了它的意图但是我怎么知道哪个警报被删除以及它与数据库中的哪个意图相匹配?是否有更好的方法为此警报执行计时器?

我还没有学过数据库课程。所以我对数据库有困难。任何建议都表示赞赏。

0 个答案:

没有答案