【发布时间】:2014-11-26 15:04:38
【问题描述】:
我正在尝试创建一个设置页面,用户可以在其中设置通知时间。
我以这种方式使用时间选择器获取时间:
public void onClick(View v) {
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
mTimePicker = new TimePickerDialog(Impostazioni.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
ViewOra.setText( selectedHour + ":" + selectedMinute);
ora=selectedHour; minuti=selectedMinute;
setNotifiche(selectedHour,selectedMinute); //i call the method set notification
}
}, hour, minute, true);//Yes 24 hour time
mTimePicker.setTitle("Select Time");
mTimePicker.show();
Intent notifiche = new Intent(Impostazioni.this , Notifiche.class);
这是我创建警报管理器的方法 setNotification
public void setNotifiche(int h,int m) {
int hour=h; int minute=m;
Intent myIntent = new Intent(this , Notifiche.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(Impostazioni.this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent);
}
最后,这是我放置通知的服务 Notifiche.class:
public void onCreate() {
Toast.makeText(this, "Notifiche", Toast.LENGTH_LONG).show();
Intent intent = new Intent(this,Impostazioni.class);
PendingIntent pending = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher).setContentTitle("Test").setContentText("Attempt n1").setContentIntent(pending);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0, mBuilder.build());
}
代码似乎工作,我的意思是当我午餐应用程序并在工作的设置页面中设置特定时间时,但如果我尝试再次设置没有任何反应。我哪里错了?
【问题讨论】:
标签: android android-intent notifications android-service alarmmanager