无法同步对 notify() 的调用 [英] Can't synchronize call to notify()
本文介绍了无法同步对 notify() 的调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请原谅我,但我无法调用 NotificationBuilder 的 notify() 方法.我有这个代码:
Forgive me, but i am not able to get a call on NotificationBuilder's notify() method. I have this code:
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();
}
}
}
无论我把同步块放在哪里,我都会遇到同样的异常:
And no matter where i put the synchronized block, i get the same exception:
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)
这可能是一个菜鸟问题,但是...我必须做些什么才能不收到该异常?谢谢.
It is maybe a noob question but... what I must to do to not receive that Exception? thank you.
推荐答案
假设您正在尝试将通知添加到 android 通知抽屉.. 您需要执行以下操作:
Assuming that you are trying to add notification to the android notification drawer.. You need to do following:
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);
这篇关于无法同步对 notify() 的调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文