无法同步对notify()的调用 [英] Can't synchronize call to notify()

查看:126
本文介绍了无法同步对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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆