我正在尝试使用有关启动新事件的通知创建Android应用程序,待办事项列表。为节目通知创建服务,但它不起作用。服务启动但通知未显示。请帮忙。(从数据库获取数据正在运行)。 在onStartCommand中调用的方法
void timeToDoSomething() {
new Thread(new Runnable() {
@Override
public void run() {
Realm readData = Realm.getDefaultInstance();
EventDTO eventsDTO;
while (true) {
Notification.Builder builder = new Notification.Builder(getApplicationContext());
Intent intent = new Intent(getApplicationContext(), MainMenuActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
GregorianCalendar gregorianCalendar = new GregorianCalendar();
String date = String.valueOf(gregorianCalendar.get(Calendar.DATE))
+ "-" + String.valueOf(gregorianCalendar.get(Calendar.MONTH) + 1)
+ "-" + String.valueOf(gregorianCalendar.get(Calendar.YEAR));
GregorianCalendar.getInstance();
SimpleDateFormat formating = new SimpleDateFormat("HH:mm");
eventsDTO = readData.where(EventDTO.class).equalTo("date", date)
.and().equalTo("timeStart", formating.format(gregorianCalendar.getTime())).findFirst();
if(eventsDTO!=null) {
builder.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.main_logo)
.setLargeIcon(BitmapFactory.decodeResource(getApplication().getResources(), R.drawable.main_logo))
.setTicker("Time to do something")
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle("Time Manager Remember")
.setContentText(eventsDTO.getName());
Notification notification = builder.build();
nm.notify(NOTIF_ID, notification);
}
try {
TimeUnit.SECONDS.sleep(60);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();
}