不建议使用构造函数

 D之phper 发布于 2023-02-13 13:57

我创建了一个android应用程序.在我的java类代码中,我收到一些消息:"构造函数Notification(int,CharSequence,long)已被弃用".应用程序一切正常我尝试运行应用程序时没有问题.我只是想知道为什么这条消息出现了.我的java类中的代码是:

public class Notifications extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notifications);

        Button b = (Button) findViewById(R.id.bNotifications);
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                Notification notify = new Notification(
                        android.R.drawable.stat_notify_more,
                        "This is important", System.currentTimeMillis());
                Context context = Notifications.this;
                CharSequence title = "You have been notified";
                CharSequence details = "Continue with what you have doing";
                Intent intent = new Intent();
                PendingIntent pending = PendingIntent.getActivity(context, 0,
                        intent, 0);
                notify.setLatestEventInfo(context, title, details, pending);
                nm.notify(0, notify);

            }
        });
    }

}

Heinzi.. 13

看看文档:

public Notification(int icon,CharSequence tickerText,long when)
在API级别1中添加

在API级别11中不推荐使用此构造函数.
请改用Notification.Builder.

据我所知,这将是相应的调用Notification.Builder:

Context context = Notifications.this;
Notification notify = new Notification.Builder(context)
     .setTicker("This is important")
     .setSmallIcon(android.R.drawable.stat_notify_more)
     .setWhen(System.currentTimeMillis())
     .build();

如您所见,Notification.Builder在设置各种通知属性和提高代码可读性方面提供了更大的灵活性,这可能是Notification构造函数被弃用的原因.

1 个回答
  • 看看文档:

    public Notification(int icon,CharSequence tickerText,long when)
    在API级别1中添加

    在API级别11中不推荐使用此构造函数.
    请改用Notification.Builder.

    据我所知,这将是相应的调用Notification.Builder:

    Context context = Notifications.this;
    Notification notify = new Notification.Builder(context)
         .setTicker("This is important")
         .setSmallIcon(android.R.drawable.stat_notify_more)
         .setWhen(System.currentTimeMillis())
         .build();
    

    如您所见,Notification.Builder在设置各种通知属性和提高代码可读性方面提供了更大的灵活性,这可能是Notification构造函数被弃用的原因.

    2023-02-13 13:59 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有