在notify()之前通知导致对象未被线程锁定

时间:2014-08-13 20:16:14

标签: android android-notifications

我正在尝试在后台服务中启动通知,这也是位置侦听器。 我有一个功能:

public Notification CreateNotification(){


        Intent notificationIntentStop = new Intent(this.getApplicationContext(), StopService.class);
        PendingIntent contentIntentStop = PendingIntent.getActivity(this.getApplicationContext(), 0, notificationIntentStop, 0);


        Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
        Shortcuts shorts = new Shortcuts(this);
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.logo)
                .setContentTitle("Localarm Running")
                .setLargeIcon(largeIcon);
        mBuilder.addAction(R.drawable.ico, "Stop", contentIntentStop);
        mBuilder.setContentText("Awake Me Up running.");
        mBuilder.setPriority(Notification.PRIORITY_MAX);
        Notification bui = mBuilder.build();

        bui.flags|= Notification.FLAG_NO_CLEAR;
            Intent notificationIntent = new Intent(this.getApplicationContext(), Intro.class);
            PendingIntent contentIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, notificationIntent, 0);
            bui.contentIntent = contentIntent;

        return bui;
    }

在onStart中我打电话:

createNotification.notify();

但是我收到以下错误:

"对象在notify()"

之前未被线程锁定

我该如何解决这个问题? 它实际上需要被调用一次并且继续运行。

3 个答案:

答案 0 :(得分:3)

notify和wait是两个用于并发的java方法,为了使用android通知你必须调用

Notification noti = CreateNotification();

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    noti.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, noti);

有关更多参考资料,请查看http://www.vogella.com/tutorials/AndroidNotifications/article.html

答案 1 :(得分:0)

您正在调用Java Object类的通知:

http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html

答案 2 :(得分:0)

错误的notify()方法。

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);        
    notificationManager.notify(0, CreateNotification());