【问题标题】:Android Status bar notification with NotificationCompat.Builder. Correct way to implement?带有 NotificationCompat.Builder 的 Android 状态栏通知。正确的实施方式?
【发布时间】:2014-04-04 18:13:32
【问题描述】:

我一直在阅读有关如何正确执行状态栏通知的信息,但大多数解决方案都使用了已弃用的功能。我认为 NotificationCompat.Builder 是解决我的问题的最佳和最新的解决方案。谁能给我一个示例代码?

对于像我这样的新手来说,我发现的最好的例子似乎不太清楚。不知道是必须在函数中实现代码并调用还是必须将代码粘贴到活动区。

Notifications with NotificationCompat.Builder

提前感谢您的帮助和时间。

【问题讨论】:

  • 转到这个:AndroidNotifications
  • 感谢您的帮助,终于我的应用程序运行良好。谢谢!!! @SimplePlan

标签: android notifications statusbar


【解决方案1】:

最后是创建简单通知的方法:

public void createNotification() {


        // Build notification
        // Actions are just fake
        Notification noti = new Notification.Builder(this)
            .setContentTitle("Advice")
            .setContentText("blahblahblahblah.").setSmallIcon(R.drawable.icon).build();

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // hide the notification after its selected
        noti.flags |= Notification.FLAG_AUTO_CANCEL;

        notificationManager.notify(0, noti);

      }

它会引发“调用需要 API 级别 11”错误,但通过右键单击项目 --> Android 工具 --> Clean Lint Markers 解决了问题。

【讨论】:

    猜你喜欢
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多