这两者有什么区别?
我想使用 startForeground 方法,但不能将其与 NotificationManager 一起使用。
谢谢
通知是一个类,它表示状态栏中的持久图标,可通过启动器访问,打开或闪烁设备上的 LED,或者通过闪烁背光、播放声音或振动来提醒用户。
通知管理器是允许您向系统添加通知的类,
startForeground
是Serivce
类的方法。例如,在您的服务类中,您可以有这样的东西。
Intent notificationIntent = new Intent(this, ActivityMain.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_stat_play)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setTicker(getString(R.string.app_name))
.setWhen(System.currentTimeMillis())
.setOngoing(true)
.setContentTitle(getString(R.string.app_name))
.setContentText(someText);
Notification notification = builder.build();
startForeground(1, notification);
Notification
是你想要发生什么来提醒用户某事的描述 - 状态栏中的图标是什么,要播放什么铃声等。
NotificationManager
是一个可以显示Notification
的系统服务。
我想使用 startForeground 方法,但不能将其与 NotificationManager 一起使用
正确。使用 Notification.Builder
(或NotificationCompat.Builder
)创建Notification
。有关使用 startForeground()
的示例,请参阅此项目。