每天和每天多次设置重复闹钟

时间:2015-06-14 13:02:02

标签: android android-alarms

我有一个场景,使用时间选择器在时间设置闹钟。我想重复报警说2小时4小时6小时。我已经完成了这个场景。但是我想每天开始这个重复的闹钟。

这是我的代码。

public void saveButtonClick(View view){

        name = drugName.getText().toString();
        doseQuantity = doseNumber.getText().toString();
        createPendingIntent(name, doseQuantity);

        if (checkBox.isChecked()){
            setRepeatingReminders();
        } else {
            createReminders();
        }
        Toast.makeText(getApplicationContext(), "You have successfully added", Toast.LENGTH_SHORT).show();
    }

    private void createPendingIntent(String drugName, String doseQuantity){
        Random random = new Random();
        int m = random.nextInt(9999 - 1000) + 1000;
        intent = new Intent(MainActivity.this, NotificationReceiver.class);
        intent.putExtra("Id", m);
        intent.putExtra("Drug Name", drugName);
        intent.putExtra("Dose", doseQuantity);
        pendingIntent = PendingIntent.getBroadcast(MainActivity.this, m, intent, 0);
    }

    private void createReminders(){
        alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeInMillisecond, AlarmManager.INTERVAL_DAY, pendingIntent);
    }

    private void setRepeatingReminders(){

        alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        long twoHours = 2 * 60 * 60 * 1000L;
        long fourHours = 4 * 60 * 60 * 1000L;
        long sixHours = 6 * 60 * 60 * 1000L;

        if (spinnerValue.equals("2 hours")){
            alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, timeInMillisecond, twoHours, pendingIntent);
        } else if (spinnerValue.equals("2min")){
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeInMillisecond, fourHours, pendingIntent);
        } else if (spinnerValue.equals("3min")){
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeInMillisecond, sixHours, pendingIntent);
        }
    }

这是我用过的接收器类

public class NotificationReceiver extends BroadcastReceiver {

    private Intent intent1;
    private PendingIntent pendingIntent;
    private int notificationId;
    private String drugName, dose;

    @Override
    public void onReceive(Context context, Intent intent) {

        intent1 = new Intent(context, MainActivity.class);

        Log.v("Id", String.valueOf(notificationId));

        drugName = intent.getStringExtra("Drug Name");
        dose = intent.getStringExtra("Dose");
        notificationId = intent.getIntExtra("Id", -1);
        pendingIntent = PendingIntent.getActivity(context, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);

        // Build the notification
        Notification.Builder notificationBuilder = new Notification.Builder(context)
                .setContentText("Hey, It's time to take your " + drugName + " Dose " + dose)
                .setContentTitle("Take Your Pill")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);

        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        // Pass the notification to the notification manager
        mNotificationManager.notify(notificationId, notificationBuilder.build());
    }
}

请有人帮助我。

0 个答案:

没有答案