From android O you cant create simple background service, it has to be foreground, so I followed a tutorial and built one, but, I got this error:
Unable to start service ...SMSService@302fa17 with Intent
{ cmp=...SMSService (has extras) }: java.lang.IllegalMonitorStateException:
object not locked by thread before notify()
I saw some other topics on this, but those are all for old Notifications
Here is my code:
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();
When you try to call notify
on specific object it wakes up a single thread that is waiting on this object's monitor.
Since you are not locking any thread, you don't have to use notify()
. Remove mNotification.notify();
.
As for displaying notification startForeground(1, notification);
is enough