Java 通知不会在选定的时间发生
我想在用户选择时间时设置通知。所以我添加了时间选择器对话框。我用日历定了时间 但我没有收到任何通知,虽然我已经确定了时间。我有三个时间选择器对话框,因为我正在设置事件的开始时间和结束时间,现在我想设置通知时间 当我设置通知时间时,同时设置结束时间 这是设置时间的代码:Java 通知不会在选定的时间发生,java,android,set,android-notifications,android-timepicker,Java,Android,Set,Android Notifications,Android Timepicker,我想在用户选择时间时设置通知。所以我添加了时间选择器对话框。我用日历定了时间 但我没有收到任何通知,虽然我已经确定了时间。我有三个时间选择器对话框,因为我正在设置事件的开始时间和结束时间,现在我想设置通知时间 当我设置通知时间时,同时设置结束时间 这是设置时间的代码: @Override protected Dialog onCreateDialog(int id) { switch (id) { case TIME_DIALOG_ID: /
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
// set time picker as current time
return new TimePickerDialog(this, timePickerListener, hour, minute,
false);
case TIME_DIALOG:
// set time picker as current time
return new TimePickerDialog(this, timePickerListener, hour, minute,
false);
case TIME:
return new TimePickerDialog(this, timePickerListener, hour, minute,
false);
}
return null;
}
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minutes) {
// TODO Auto-generated method stub
hour = hourOfDay;
minute = minutes;
updateTime(hour, minute);
}
};
// Used to convert 24hr format to 12hr format with AM/PM values
private void updateTime(int hour, int minute) {
df = new SimpleDateFormat("HH:mm a");
if (timeType == 0) {
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.DATE, day);
datefrom = c.getTime();
startTime = String.valueOf(datefrom);
aTime = df.format(datefrom.getTime());
showFromTime.setText(aTime);
Toast.makeText(getApplicationContext(), String.valueOf(datefrom), Toast.LENGTH_LONG).show();
} else if (timeType == 1) {
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.DATE, day);
dateto = c.getTime();
endTime = String.valueOf(dateto);
bTime = df.format(dateto.getTime());
showToTime.setText(bTime);
Toast.makeText(getApplicationContext(), String.valueOf(dateto), Toast.LENGTH_LONG).show();
}
else
{
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.DATE, day);
notification = c.getTime();
notificationTime = df.format(notification);
Toast.makeText(getApplicationContext(),notificationTime,Toast.LENGTH_SHORT).show();
}
}
通知服务类
public class NotificationReceiver extends BroadcastReceiver {
private static final int MY_NOTIFICATION_ID = 0;
NotificationManager notificationManager;
Notification myNotification;
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Time is set", Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
context,
0,
myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
myNotification = new NotificationCompat.Builder(context)
.setContentTitle("Event Time")
.setContentText("Time")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_alarm_black_48dp)
.build();
notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
}
}
编辑:设置报警功能
@SuppressLint("NewApi")
private void setAlarm(Calendar targetmCalen) {
AlarmManager alarmMgr;
PendingIntent alarmIntent;
alarmMgr = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), NotificationReceiver.class);
alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
Toast.makeText(getBaseContext(),
"call alarmManager.setExact()",
Toast.LENGTH_LONG).show();
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,targetmCalen.getTimeInMillis(),
1000 * 60 * 20, alarmIntent);
ComponentName receiver = new ComponentName(getApplicationContext(),NotificationReceiver.class);
PackageManager pm = getApplicationContext().getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
// Toast.makeText(getApplicationContext(),"Notification Set",Toast.LENGTH_SHORT).show();
}
请帮帮我。哦,惊喜!你没读那本书!特别是,上面写着:
注意:从API 19(KITKAT)开始,警报传递是不准确的:操作系统将切换警报,以最大限度地减少唤醒和电池使用。
并且它还提供了漫游。尝试使用setExact(),仍然没有通知@Hrundi V.bakshi根据您当前的操作系统版本,可以使用set()
或setExact()
嗯,可能有一些代码不太合适。请尝试遵循android.developer网站上的重复报警示例。它工作得很好。另一段您可能会感兴趣的漂亮代码是TaskButler。很好的示例..帮助很大。谢谢……哦,惊喜!你没读那本书!特别是,上面写着:注意:从API 19(KITKAT)开始,警报传递是不准确的:操作系统将切换警报,以最大限度地减少唤醒和电池使用。
并且它还提供了漫游。尝试使用setExact(),仍然没有通知@Hrundi V.bakshi根据您当前的操作系统版本,可以使用set()
或setExact()
嗯,可能有一些代码不太合适。请尝试遵循android.developer网站上的重复报警示例。它工作得很好。另一段您可能会感兴趣的漂亮代码是TaskButler。很好的示例..帮助很大。非常感谢。