原谅我,但我无法接到NotificationBuilder的notify()方法的电话。我有这段代码:
timer.scheduleAtFixedRate(
new TimerTask() {
@Override
public void run() {
synchronized (this) {
Builder notif = new NotificationCompat.Builder(c);
notif.setContentIntent(contIntent)
.setWhen(System.currentTimeMillis())
.setTicker("Notification ticker")
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Vinceri")
.setContentText("Ha recibido una oferta de trabajo")
.setAutoCancel(true);
notif.notify();
}
}
}
无论我把同步块放在哪里,我都会得到同样的例外:
04-05 10:42:55.116: E/AndroidRuntime(18244): FATAL EXCEPTION: Timer-0
04-05 10:42:55.116: E/AndroidRuntime(18244): java.lang.IllegalMonitorStateException: object not locked by thread before notifyAll()
04-05 10:42:55.116: E/AndroidRuntime(18244): at java.lang.Object.notifyAll(Native Method)
04-05 10:42:55.116: E/AndroidRuntime(18244): at com.publidirecta.vinceriazafata.NotificationService$1.run(NotificationService.java:126)
04-05 10:42:55.116: E/AndroidRuntime(18244): at java.util.Timer$TimerImpl.run(Timer.java:284)
这可能是一个noob问题,但是......我必须做些什么来不接受这个例外?谢谢。
答案 0 :(得分:3)
假设您正在尝试向android通知抽屉添加通知..您需要执行以下操作:
Builder notif = new NotificationCompat.Builder(c);
notif.setContentIntent(contIntent)
.setWhen(System.currentTimeMillis())
.setTicker("Notification ticker")
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Vinceri")
.setContentText("Ha recibido una oferta de trabajo")
.setAutoCancel(true);
//create notification from builder
Notification notification = notif.build();
//get instance of NotificationManager
NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//call notify method of NotificationManager to add this notification to android notification drawer..
notificationmanager.notify(0, notification);