如何准确地使用Notification.Builder

105

我发现我在使用通知(notification)时使用了一个已弃用的方法(notification.setLatestEventInfo())。

它建议使用Notification.Builder。

  • 我该如何使用它?

当我尝试创建一个新实例时,它告诉我:

Notification.Builder cannot be resolved to a type

我注意到这个链接从API Level 11(Android 3.0)开始可用。 - mobiledev Alex
请查看以下更新的答案:(链接) - Amit Vaghela
11个回答

0
          // This is a working Notification
       private static final int NotificID=01;
   b= (Button) findViewById(R.id.btn);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Notification notification=new       Notification.Builder(MainActivity.this)
                    .setContentTitle("Notification Title")
                    .setContentText("Notification Description")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .build();
            NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
            notification.flags |=Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify(NotificID,notification);


        }
    });
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接