在前台服务通知中的 notify() 之前,对象未被线程锁定

object not locked by thread before notify() in Foreground Service Notification

来自 android 哦,你不能创建简单的后台服务,它必须是前台的,所以我按照教程构建了一个,但是,我得到了这个错误:

    Unable to start service ...SMSService@302fa17 with Intent 
{ cmp=...SMSService (has extras) }: java.lang.IllegalMonitorStateException: 
object not locked by thread before notify()

我看到了一些关于此的其他主题,但这些都是针对旧通知的

这是我的代码:

Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent mPendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        Notification mNotification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Content Title")
                .setContentText("Content Text")                                                                                 
                .setSmallIcon(R.drawable.ic_check)
                .setContentIntent(mPendingIntent)
                .build();

            startForeground(1, mNotification);
            mNotification.notify();

当您尝试在特定对象上调用 notify 时,它会唤醒正在等待该对象监视器的单个线程。

由于您没有锁定任何线程,因此不必使用 notify()。删除 mNotification.notify();

至于显示通知startForeground(1, notification);就够了