//我从sq-lite数据库中获取数据处理程序类的数据。
public class ReminderReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm");
List<Reminder> reminderList;
Date dateobj = new Date();
//调用方法从sqlite获取列表
new DataBaseHandler(context).getAllReminderList();
reminderList = Util.reminderList;
for (int i = 0; i < reminderList.size(); i++) {
Reminder reminder = reminderList.get(i);
if (df.format(dateobj).equalsIgnoreCase(reminder.getDatetime())) {
try {
new DataBaseHandler(context).deleteReminder(reminder.getTaskid());
Intent intent1 = new Intent(context, TaskDetail.class);
intent1.putExtra("id", reminder.getTaskid());
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent1, 0);
String taskName = reminder.getTaskname();
//显示通知
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alert == null) {
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
Notification.Builder builder = new Notification.Builder(context);
NotificationManager manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Notification myNotication;
builder.setSmallIcon(R.drawable.st);
builder.setContentTitle("SharedTasklist Notification");
builder.setTicker("SharedTasklist Reminder");
builder .setContentText("It's time to do " + taskName);
builder .setAutoCancel(true);
builder .setOngoing(true);
builder .setContentIntent(pendingIntent);
builder .setSound(alert); //This sets the sound to play
builder.build();
builder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
myNotication = builder.getNotification();
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
manager.notify(m, myNotication);
//我检查所有其他课程,它没问题。这里显示了在通知之前对象未锁定线程的错误..
alert.notify();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}