Android,多个带时间选择器的闹钟

时间:2015-02-24 17:12:09

标签: android alarmmanager timepicker android-alarms android-timepicker

我试图将时间选择器用于当天用餐的5个警报,真相调查很多,我发现的有点令人困惑。 我这样做的方式不是很“有效”,但在这件事上没有经验,我想不出来。 现在的问题是,如果我发出5个警报,但是当我认为分开时,有时其中一个或两个不起作用。 例: 我有一个RelativeLayout,他们在TextView中显示警报,所以: 报警1 闹钟2 闹钟3 闹钟4 警报5

警报2有时不起作用或2和4.这种方式相隔一分钟。

事实是,代码不是很好,但仍然应该创建5并运行但是就是这样。

我希望有人能帮助我。

谢谢

这是代码和类

public class Recordatorios2 extends Activity {

Button buttonstartSetDialog;
TextView desayuno,merienda1,almuerzo,merienda2,cena;
TimePickerDialog timePickerDialog;
private Spinner alarma;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recordatorios2);
    llenarSpinnerAlarmas();

    desayuno = (TextView)findViewById(R.id.Desayuno);
    merienda1 = (TextView) findViewById(R.id.Merienda1);
    almuerzo = (TextView) findViewById(R.id.Almuerzo);
    merienda2 = (TextView) findViewById(R.id.Merienda2);
    cena = (TextView) findViewById(R.id.cena);

    alarma = (Spinner) findViewById(R.id.spinnerAlarma);
    buttonstartSetDialog = (Button)findViewById(R.id.startSetDialog);
    buttonstartSetDialog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          //  desayuno.setText("");
            openTimePickerDialog(false);
        }
    });
     }


 public void llenarSpinnerAlarmas(){

    alarma = (Spinner) findViewById(R.id.spinnerAlarma);
    //Creamos el adaptador
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.Alarmas,android.R.layout.simple_spinner_item);
    //Añadimos el layout para el menú
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    //Le indicamos al spinner el adaptador a usar
    alarma.setAdapter(adapter);
}

private void openTimePickerDialog(boolean is24r) {
    Calendar calendar = Calendar.getInstance();

    timePickerDialog = new TimePickerDialog(
            Recordatorios2.this,
            onTimeSetListener,
            calendar.get(Calendar.HOUR_OF_DAY),
            calendar.get(Calendar.MINUTE),
            is24r);
    timePickerDialog.setTitle("Elija la alarma");

    timePickerDialog.show();
}

TimePickerDialog.OnTimeSetListener onTimeSetListener
        = new TimePickerDialog.OnTimeSetListener(){

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {


        Calendar calNow = Calendar.getInstance();
        Calendar calSet = (Calendar) calNow.clone();

       calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
        calSet.set(Calendar.MINUTE, minute);
        calSet.set(Calendar.SECOND, 0);
        calSet.set(Calendar.MILLISECOND, 0);


        if(calSet.compareTo(calNow) <= 0){
            calSet.add(Calendar.DATE, 1);
        }

        setAlarm(calSet);
    }};

private void setAlarm( Calendar targetCal) {

    if(alarma.getSelectedItem().toString().equals("Desayuno")){

        RelativeLayout recordatorios = (RelativeLayout) findViewById(R.id.recordatoriosLayout);
        desayuno.setText("Desayuno: " + targetCal.getTime());
        Intent intent1 = new Intent(getBaseContext(), AlarmReceiver.class);
        PendingIntent pendingIntent1 = PendingIntent.getBroadcast(getBaseContext(),1 , intent1, 0);
        AlarmManager alarmManager1 = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        alarmManager1.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent1);

    }else if(alarma.getSelectedItem().toString().equals("Primera Merienda")){
        RelativeLayout recordatorios = (RelativeLayout) findViewById(R.id.recordatoriosLayout);
        merienda1.setText("Primer Merienda: " + targetCal.getTime());
        Intent intent2 = new Intent(getBaseContext(), AlarmReceiver.class);
        PendingIntent pendingIntent2 = PendingIntent.getBroadcast(getBaseContext(),2 , intent2,0);
        AlarmManager alarmManager2 = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        alarmManager2.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent2);

    }else if(alarma.getSelectedItem().toString().equals("Almuerzo")){
        RelativeLayout recordatorios = (RelativeLayout) findViewById(R.id.recordatoriosLayout);
        almuerzo.setText( "Almuerzo: " + targetCal.getTime());
        Intent intent3 = new Intent(getBaseContext(), AlarmReceiver.class);
        PendingIntent pendingIntent3 = PendingIntent.getBroadcast(getBaseContext(),3 , intent3, 0);
        AlarmManager alarmManager3 = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        alarmManager3.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent3);

    }else if(alarma.getSelectedItem().toString().equals("Segunda Merienda")){
        RelativeLayout recordatorios = (RelativeLayout) findViewById(R.id.recordatoriosLayout);
        merienda2.setText( "Segunda Merienda: " + targetCal.getTime());
        Intent intent4 = new Intent(getBaseContext(), AlarmReceiver.class);
        PendingIntent pendingIntent4 = PendingIntent.getBroadcast(getBaseContext(),4 , intent4,0);
        AlarmManager alarmManager4 = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        alarmManager4.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent4);

    } else if(alarma.getSelectedItem().toString().equals("Cena")){
        RelativeLayout recordatorios = (RelativeLayout) findViewById(R.id.recordatoriosLayout);
        cena.setText("Cena: " + targetCal.getTime());
        Intent intent5 = new Intent(getBaseContext(), AlarmReceiver.class);
        PendingIntent pendingIntent5 = PendingIntent.getBroadcast(getBaseContext(),5 , intent5, 0);
        AlarmManager alarmManager5 = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        alarmManager5.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent5);


}
}



}

现在在这个课程中,我想指定每个警报何时发送不同的消息但鼻子获取每个警报的每个意图的id或标识符,我该怎么办?

我会试着尝试制作i.getId或类似的东西,但显然不能。

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context arg0, Intent i) {
        Toast.makeText(arg0, "alarma", Toast.LENGTH_LONG).show();

}

0 个答案:

没有答案