我加入報警我的應用程序設置。當用戶使用timepicker設置預約時間時,我想警告用戶。如果我設置報警,只需要最後報警我設置,我多麼想使這兩個方面的工作?..報警使用timepicker不工作,機器人
沒有錯誤,我在Manefest已經註冊。其實我想獲取日期和時間設置sqlite的,報警是基於日期和time.i'm卡住,首先我要用戶添加約會,讓說25/4/2013下午4:30上..數據保存在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);
}
}
任何錯誤,而達到特定的報警時間,還是你在註冊清單中的接收器? – Kanth 2013-04-26 04:20:46
沒有錯誤,我已經在Manefest註冊了。實際上我想獲取在sqlite中設置的日期和時間,並且鬧鐘基於日期和時間。我覺得我的代碼是功能setAlarm錯的..我越來越堅持,首先我要用戶添加約會,讓說25/4/2013下午4:30上。該數據是保存在sqlite的。那麼當天,我想要應用程序,以警告用戶..我怎麼能這樣做?我知道使用報警管理器,但我不知道從哪裏代碼 – new 2013-04-26 10:24:46
ok了,它已經可以到主菜單啓動,爲alarmreceiver類,我可以做麪包或alertdialog而不是更改活動? – new 2013-04-26 11:16:36