Android中的Notification和NotificationManger有什么区别?

时间:2014-02-16 17:12:38

标签: android notifications

这两者有什么区别?

我想使用startForeground方法,不能将它与NotificationManager一起使用..

由于

2 个答案:

答案 0 :(得分:0)

Notification描述了您想要提醒用户注意某事的内容 - 状态栏中的图标,播放的铃声等等。

NotificationManager是一种可以显示Notification

的系统服务
  

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

正确。使用Notification(或Notification.Builder)创建NotificationCompat.Builder。有关使用startForeground()

的示例,请参阅this project

答案 1 :(得分:0)

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

通知管理器是允许您向系统添加通知的类,

startForegroundSerivce类的方法。例如,在您的Service类中,您可以拥有类似的内容。

    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);
相关问题