警报设置使用timepicker不工作,android

时间:2013-04-26 03:19:10

标签: android timepicker android-alarms

我正在为我的应用添加闹钟。当用户使用timepicker设置约会时间时,我想提醒用户报警。如果我设置为闹钟,它只需要我设置的最后一个闹钟,我想如何使两个都工作?..

没有错误,我已经在Manefest注册了。实际上我想要获取在sqlite中设置的日期和时间,并且警报基于日期和时间。我被卡住了,首先我希望用户添加约会,让我们说下午4点30分2013年4月4日..数据保存在sqlite中。那天,我想要应用程序,警告用户..我怎么能这样做?我知道使用报警管理器,但我不知道从代码开始的地方

并且对于alarmReceiver,我希望班级能够使用警报或祝酒或通知来提醒用户。但我已尝试提醒并且不能使用烤面包。还有其他方法吗? 请帮我 下面是我的代码:

public class AppointmentAdd extends Activity 
{

private SQLiteDatabase database;
private DBHelper helper;
private ScheduleClient scheduleClient;
Button btnSave, btnDate, btnTime;
EditText addPurpose;

DateFormat fmtDateAndTime = DateFormat.getDateTimeInstance();
Calendar myCalendar = Calendar.getInstance();

DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
        int dayOfMonth) {
    myCalendar.set(Calendar.YEAR, year);
    myCalendar.set(Calendar.MONTH, monthOfYear);
    myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
    updateLabelDate(year, monthOfYear, dayOfMonth);
}
};

TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    myCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
    myCalendar.set(Calendar.MINUTE, minute);
    updateLabelTime(hourOfDay, minute);
}
};

private void updateLabelDate(int year, int monthOfYear,
        int dayOfMonth) {

    year = myCalendar.get(Calendar.YEAR);
    monthOfYear = myCalendar.get(Calendar.MONTH);
    dayOfMonth = myCalendar.get(Calendar.DATE);

    btnDate.setText(new StringBuilder().append(dayOfMonth).append(".")
            .append(monthOfYear + 1).append(".").append(year).append(" "));
}

private void updateLabelTime(int hourOfDay, int minute) {

    hourOfDay = myCalendar.get(Calendar.HOUR_OF_DAY);
    minute = myCalendar.get(Calendar.MINUTE);
    btnTime.setText(new StringBuilder().append(hourOfDay).append(":")
            .append(minute));
}

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

    btnSave = (Button)findViewById(R.id.button3);
    btnDate = (Button)findViewById(R.id.button1);
    btnTime = (Button)findViewById(R.id.button2);
    addPurpose = (EditText)findViewById(R.id.editText1);

    scheduleClient = new ScheduleClient(this);
    scheduleClient.doBindService();

    btnDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            new DatePickerDialog(AppointmentAdd.this, d, myCalendar
                    .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH)).show();
        }
    });

    btnTime.setOnClickListener(new View.OnClickListener() {
        public  void onClick(View v) {
            new TimePickerDialog(AppointmentAdd.this, t, myCalendar
                    .get(Calendar.HOUR_OF_DAY), myCalendar
                    .get(Calendar.MINUTE), true).show();
        }

    });


    helper = new DBHelper(this);
    database = helper.getWritableDatabase();

    btnSave.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            String purpose = addPurpose.getText().toString();
            String date = btnDate.getText().toString();
            String time = btnTime.getText().toString();

            helper.insertDataAppointment(database, date, time, purpose);
            Toast.makeText(AppointmentAdd.this, "Successfully Add", Toast.LENGTH_SHORT).show();

            setAlarm(myCalendar.get(Calendar.HOUR_OF_DAY), myCalendar.get(Calendar.MINUTE));

            Intent i = new Intent(AppointmentAdd.this, AppointmentView.class);
            startActivity(i);
        }
    });

    updateLabelDate(0, 0, 0);
    updateLabelTime(0, 0);
}

private void setAlarm(int Hour, int Minute){
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, Hour);
    cal.set(Calendar.MINUTE, Minute);
    cal.set(Calendar.SECOND, 0);

    //in case of choosing a previous hour, then set alarm to next day
    if (cal.getTimeInMillis() < System.currentTimeMillis())
        cal.set(Calendar.HOUR_OF_DAY, Hour + 24);

    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent Notifyintent = new Intent(this, Notify.class);
PendingIntent Notifysender = PendingIntent.getBroadcast(this, 0, Notifyintent, PendingIntent.FLAG_UPDATE_CURRENT);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 3600000, Notifysender);

    Toast.makeText(AppointmentAdd.this, "Alarm set successfully" , Toast.LENGTH_LONG).show();
}

public void onClickCancel(View v){
    startActivity (new Intent(getApplicationContext(), AppointmentView.class));
}

} // end class

这里是我的AlarmReceiver类:

public class AlarmReceiver extends BroadcastReceiver {

@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
      NotificationManager myNotificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
      Notification notification = new Notification(R.drawable.ic_launcher, "Update Device", 0);
      Intent notificationIntent = new Intent(context, AppointmentView.class);
      PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
      notification.setLatestEventInfo(context, "Appointment", "Please check your appointment list", contentIntent);
      notification.flags |= Notification.FLAG_HIGH_PRIORITY;
      myNotificationManager.notify(0, notification);
}
}

2 个答案:

答案 0 :(得分:0)

根据您的评论:

这是因为pendingintent每次都需要不同的请求ID

现在你现在的PendingIntent是以下方式:

PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

在这里,您每次都将reqid设为0,因此不会创建不同的待处理意图,而是覆盖当前的待处理意图,这就是您获取最新设置警报的原因。因此,每次都给出具有不同编号的唯一请求ID,如下所示。

 PendingIntent pi = PendingIntent.getBroadcast(this, reqid, intent,PendingIntent.FLAG_UPDATE_CURRENT);

即使我按照您的要求做了同样的事情,我必须将整个数据存储在sqlite数据库中,关于消息和警报。因此,为了每次设置不同的request id,我使用了数据库中的主键id来设置挂起的意图。希望这可以帮助。

答案 1 :(得分:0)

你可以试试这个:

final int alarmId = (int) System.currentTimeMillis();
PendingIntent pi =
  PendingIntent.getBroadcast(Date_Time.this, alarmId, i,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager manager = (AlarmManager) getSystemService(Date_Time.this.ALARM_SERVICE);
manager.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis() , pi);