1

原谅我,但我无法调用 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)

这可能是一个菜鸟问题,但是......我必须做些什么才能不收到那个例外?谢谢你。

4

1 回答 1

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);
于 2013-04-05T08:51:52.060 回答