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

3

我将尝试在后台服务中启动通知,该服务还是位置监听器。 我有一个功能:

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个回答

4

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

该网站提供了与Android通知相关的教程和参考资料。

0

那到底是什么意思? - arleitiss
这意味着你正在执行“唤醒等待在此对象监视器上的单个线程”的操作,这与Android通知完全无关。 - Edward van Raak

0
错误的 notify() 方法。
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);        
    notificationManager.notify(0, CreateNotification());

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接