使用NotificationCompat.Builder进行Android状态栏通知。正确的实施方式?

时间:2014-03-03 15:49:52

标签: android notifications statusbar

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

我发现的最好的例子对我这样的新手来说并不清楚。我不知道是否必须在函数中实现代码并调用它,或者我必须将代码粘贴到活动区域中。

Notifications with NotificationCompat.Builder

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

1 个答案:

答案 0 :(得分: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);

      }

它会抛出“Call required API level 11”错误,但是右键单击项目 - > Android工具 - > Clean Lint Markers解决了这个问题。