关于Android o+ 隐藏通知栏
sorrybeman opened this issue · comments
YUI commented
网上查了很久,结果都是说前台服务不可以隐藏。我借鉴了该库的解决方式。但是并没有效果,这是我的代码,希望可以给个思路,谢谢了:)。
private void startForeground() {
if (KeepLive.sForegroundNotification != null) {
Intent intent = new Intent(getApplicationContext(), NotificationClickReceiver.class);
intent.setAction(NotificationClickReceiver.ACTION_CLICK_NOTIFICATION);
final Notification notification = NotificationUtils.getNotification(this, KeepLive.sForegroundNotification.getTitle(), KeepLive.sForegroundNotification.getDescription(), KeepLive.sForegroundNotification.getIconRes(), intent);
//隐藏通知栏
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(KEY_NOTIFICATION_ID, notification);
startForeground(KEY_NOTIFICATION_ID, notification);
if (manager.getNotificationChannel(notification.getChannelId()) != null) {
mHandler.post(new Runnable() {
@Override
public void run() {
manager.deleteNotificationChannel(notification.getChannelId());
}
});
}
}else {
NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
managerCompat.notify(KEY_NOTIFICATION_ID,notification);
startForeground(KEY_NOTIFICATION_ID,notification);
}
}
}