在调用startForeground之后马上调用
stopForeground(true);
完整的就是这样:
将这两句代码放到onStartCommand中
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//构建notification
startForeground(1, new NotificationCompat.Builder(this, CHANNEL_ONE_ID).build());
stopForeground(true);
//最后调用父类的onStartCommand
return super.onStartCommand(intent, flags, startId);
}