需要您的Android警报帮助

时间:2014-06-02 22:03:07

标签: android

我有不同的闹钟按钮,用于在同一页面上为不同的内容提供通知。当我尝试为第一个项目设置闹钟时会出现问题,即使我只为第一个按钮设置闹钟,所有文本视图也会有相同的时间。我可以帮忙吗?提前致谢 这是代码

@Override
protected Dialog onCreateDialog(int id) {
    Dialog dialog = null;
    switch (id) {
    case DIALOG_TIME:
        dialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {

            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Calendar c = Calendar.getInstance();
                c.setTimeInMillis(System.currentTimeMillis());
                c.set(Calendar.HOUR_OF_DAY, hourOfDay);
                c.set(Calendar.MINUTE, minute);
                c.set(Calendar.SECOND, 0);
                // Alarm  for first item button
                editTextbreakfast.setText(DateFormat.format("h:mm a", c)); // show time in the box field                                
                Intent intent1 = new Intent(dailyhabitWithAlram.this, AlarmReceiver.class);
                PendingIntent pi1 = PendingIntent.getBroadcast(dailyhabitWithAlram.this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT); // To have multiple alarms we need to add an ID to this intent , so each time it is linked to different alarm.
                AlarmManager alarmManager1 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                alarmManager1.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi1);
                Toast.makeText(dailyhabitWithAlram.this, "Alarm has been set..", Toast.LENGTH_LONG).show(); /// this to notify the user that the alarm is set
                // Alarm for second item
                editTextSnack.setText(DateFormat.format("h:mm a", c));
                Intent intent2 = new Intent(dailyhabitWithAlram.this, AlarmReceiver.class);
                PendingIntent pi2 = PendingIntent.getBroadcast(dailyhabitWithAlram.this, 1, intent2, PendingIntent.FLAG_UPDATE_CURRENT); // To have multiple alarms we need to add an ID to this intent , so each time it is linked to different alarm.
                AlarmManager alarmManager2 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                alarmManager2.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi2);
                Toast.makeText(dailyhabitWithAlram.this, "Alarm has been set..", Toast.LENGTH_LONG).show();
            }

        }, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), false);
        break;
    }
    return dialog;

} // End of switch 

这是AlarmReceiver

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, AlarmActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}

这是AlarmActivity

public class AlarmActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new AlertDialog.Builder(AlarmActivity.this).setTitle("Task").setMessage("Time for    Breakfast").setPositiveButton("ok", new OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                AlarmActivity.this.finish();
            }
        }).create().show();
    }
}

1 个答案:

答案 0 :(得分:0)

您不应该使用相同的c实例来设置两个AlarmManager.set()调用的时间(除非您在两次调用之间更改c指定的时间)。

另外,没有必要两次获得AlarmManageralarmManager1alarmManager2将是同一个实例。