繁体   English   中英

带有NotificationCompat.Builder的Android状态栏通知。 正确的实施方式?

[英]Android Status bar notification with NotificationCompat.Builder. Correct way to implement?

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

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

使用NotificationCompat.Builder进行通知

预先感谢您的帮助和时间。

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

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可以解决此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM