我创建了一个应用程序,允许用户输入分配截止日期,然后通知时间分配到期,这一切都正常,但是,我找不到一个明确的示例,说明如何制作警报在重启后仍然存在
接收器上的数据库内容只是用特定的分配信息填充通知。报警本身不会保存在数据库中,时间和日期由用户输入设置并直接传递给报警管理器。
警报创建页面:
Long nIdLong = System.currentTimeMillis();
String nId = nIdLong.toString();
final int _id = (int) System.currentTimeMillis();
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
alarmIntent.putExtra("nID", nIdLong);
// make sure the intent have id's
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, _id, alarmIntent , 0);
AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(getActivity().ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
这是我的接收者:
public void onReceive(Context context, Intent intent)
{
long dbId = intent.getLongExtra("nID", 0);
String notificationId = Long.toString(dbId);
Log.i("App", "called receiver method");
try{
userDbHelper = new UserDbHelper(context.getApplicationContext());
sqLiteDatabase = userDbHelper.getReadableDatabase();
cursor = userDbHelper.getAssignmentNotification(notificationId, sqLiteDatabase);
if (cursor.moveToFirst()) {
do {
id = cursor.getString(0);
name = cursor.getString(1);
subject = cursor.getString(2);
date = cursor.getString(3);
time = cursor.getString(4);
} while (cursor.moveToNext());
}
NotificationGenerator.generateNotification(context, date, time, name, subject);
}catch(Exception e){
e.printStackTrace();
}
}
答案 0 :(得分:0)
https://examples.javacodegeeks.com/android/core/activity/android-start-service-boot-example/ 此示例显示如何在引导时启动服务,将警报保存到数据库中,并使启动服务运行所有已保存的警报,以便在每次启动时重新创建它们。