Android中通知和NotificationManager的区别是什么?

Android中通知和NotificationManager的区别是什么?,android,notifications,Android,Notifications,这两者有什么区别 我想使用startForeground方法,但无法与NotificationManager一起使用 感谢一个通知是一个描述,说明您希望发生什么来提醒用户一些事情——状态栏中出现了什么图标,播放什么铃声,等等 NotificationManager是一种可以显示通知的系统服务 我想使用startForeground方法,但不能与NotificationManager一起使用 对。使用Notification.Builder(或NotificationCompat.Builder)

这两者有什么区别

我想使用startForeground方法,但无法与NotificationManager一起使用


感谢

一个
通知
是一个描述,说明您希望发生什么来提醒用户一些事情——状态栏中出现了什么图标,播放什么铃声,等等

NotificationManager
是一种可以显示
通知的系统服务

我想使用startForeground方法,但不能与NotificationManager一起使用


对。使用
Notification.Builder
(或
NotificationCompat.Builder
)创建
通知。有关使用
startForeground()

的示例,请参阅。通知是一个类,表示状态栏中的持久图标,可通过启动器访问,打开或闪烁设备上的LED,或通过闪烁背光、播放声音或振动来提醒用户

Notification Manager是允许您向系统添加通知的类

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);