我正在开发一个应用程序,它允许我在智能手机或智能手表(安卓服装)上显示通知。通过保存特定时间(timepicker),描述(edittext)和工作日(切换按钮),数据将保存到数据库中。
我的问题是在正确的时间显示正确的通知,这是在之前创建的。保存到数据库工作,但在正确的时间显示通知不起作用。我多次尝试使用AlarmManager解决方案。但我不太确定如何使用它。 我的数据库看起来像:
public static final String KEY_ID_ACTION = "id";
public static final String KEY_DESCRIPTION = "description";
public static final String KEY_HOUR = "time_hour";
public static final String KEY_MINUTE = "time_minute";
public static final String KEY_MONDAY = "monday";
public static final String KEY_TUESDAY = "tuesday";
public static final String KEY_WEDNESDAY = "wednesday";
public static final String KEY_THURSDAY = "thursday";
public static final String KEY_FRIDAY = "friday";
public static final String KEY_SATURDAY = "saturday";
public static final String KEY_SUNDAY = "sunday";
我的AlarmManagerBroadcastReceiver看起来像这样:
public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {
final public static String ONE_TIME = "onetime";
DatabaseHandler db;
@Override
public void onReceive(Context context, Intent intent) {
db = new DatabaseHandler(context);
PowerManager pm = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
// Acquire the lock
wl.acquire();
// You can do the processing here.
Bundle extras = intent.getExtras();
StringBuilder msgStr = new StringBuilder();
if (extras != null && extras.getBoolean(ONE_TIME, Boolean.FALSE)) {
// Make sure this intent has been sent by the one-time timer button.
msgStr.append("One time Timer : ");
}
Format formatter = new SimpleDateFormat("hh:mm:ss a");
msgStr.append(formatter.format(new Date()));
Toast.makeText(context, msgStr, Toast.LENGTH_LONG).show();
// Release the lock
wl.release();
}
public void SetAlarm(Context context, String hour, String minute) {
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance(Locale.GERMAN);
cal.set(Calendar.HOUR, Integer.valueOf(hour));
cal.set(Calendar.MINUTE, Integer.valueOf(minute));
cal.set(Calendar.SECOND, 0);
//cal.set(Calendar.DAY_OF_WEEK, 1);
cal.set(Calendar.DAY_OF_WEEK, 3);
Long alarmTime = cal.getTimeInMillis();
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(ONE_TIME, Boolean.FALSE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
// After after 5 seconds
am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime,
1000 * 5, pi);
}
public void CancelAlarm(Context context) {
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent sender = PendingIntent
.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
public void setOnetimeTimer(Context context) {
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(ONE_TIME, Boolean.TRUE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
}
}
使用以下代码,我可以设置闹钟,并且还会显示祝酒词。
public void onetimeTimer(View view) {
Context context = getActivity().getApplicationContext();
if (alarm != null) {
alarm.setOnetimeTimer(context);
} else {
Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show();
}
}
此外,工作日的使用已经有效,但只能创建一个通知,甚至不会从数据库中获取数据,也不会每周重复一次通知。我从数据库中删除通知后,不知道如何停止通知的警报。