多个状态栏通知

发布于 2024-10-26 19:53:26 字数 89 浏览 8 评论 0原文

我可以从一个程序(服务)在状态栏中创建多个通知,还是应该使用可点击的对象列表(例如 LinearLayout)创建新的 Activity?

Can I create more than one notification in status bar from one program(Service) or I should create new Activity with a clickable list(for example LinearLayout) of objects?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽你眉间 2024-11-02 19:53:26

您当然可以从服务或应用程序创建多个通知,但您必须问自己,作为用户,您是否希望应用程序向您发送垃圾邮件通知。我一直在远程服务中使用一个通知,并通过更新其内容来重复使用同一通知。下面是一个示例:

public void onPlaybackStarted(int currentTrack, Show show) {
notificationManager.cancel(R.layout.notification_playing);

notification.tickerText = show.getTracks().get(currentTrack).getName();
if (notificationView == null) {
    notificationView = new RemoteViews(getPackageName(), R.layout.notification_playing);
}
notificationView.setTextViewText(R.id.notification_playing_track, show.getTracks().get(currentTrack).getName());
notificationView.setTextViewText(R.id.notification_playing_band, show.getArtist());
notificationView.setTextViewText(R.id.notification_playing_date, show.getDate());
Intent intent = new Intent(TrackPlayerService.this, ListTracksActivity.class)
        .putExtra("track", currentTrack)
        .putExtra("artist", show.getArtist())
        .putExtra("date", show.getDate())
        .putExtra("location", show.getLocation())
        .putExtra("venue", show.getVenue())
        .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notification.contentView = notificationView;
notification.contentIntent = PendingIntent.getActivity(TrackPlayerService.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
notification.flags |= Notification.FLAG_ONGOING_EVENT;

notificationManager.notify(R.layout.notification_playing, notification);
}

如果您的通知不是循环的,这意味着您需要同时通知用户 3 或 4 件不同的事情,那么拥有一个打开 ListActivity 的通知将是最好的方法。

You certainly can create more than one notification from a service or application, but you have to ask yourself is, as a user, you would want an application to spam notifications to you. I've been using one notification in my remote service and reusing the same notification by just updating its content. Here is an example:

public void onPlaybackStarted(int currentTrack, Show show) {
notificationManager.cancel(R.layout.notification_playing);

notification.tickerText = show.getTracks().get(currentTrack).getName();
if (notificationView == null) {
    notificationView = new RemoteViews(getPackageName(), R.layout.notification_playing);
}
notificationView.setTextViewText(R.id.notification_playing_track, show.getTracks().get(currentTrack).getName());
notificationView.setTextViewText(R.id.notification_playing_band, show.getArtist());
notificationView.setTextViewText(R.id.notification_playing_date, show.getDate());
Intent intent = new Intent(TrackPlayerService.this, ListTracksActivity.class)
        .putExtra("track", currentTrack)
        .putExtra("artist", show.getArtist())
        .putExtra("date", show.getDate())
        .putExtra("location", show.getLocation())
        .putExtra("venue", show.getVenue())
        .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notification.contentView = notificationView;
notification.contentIntent = PendingIntent.getActivity(TrackPlayerService.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
notification.flags |= Notification.FLAG_ONGOING_EVENT;

notificationManager.notify(R.layout.notification_playing, notification);
}

If your notifications are not revolving, meaning you need to notify the user about 3 or 4 different things simultaneously, then having a notification that opens a ListActivity would be the best way to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文