【问题标题】:In Android, how do I remove a notification variable from being deprecated?在 Android 中,如何删除已弃用的通知变量?
【发布时间】:2013-09-26 01:56:56
【问题描述】:

如何从我的代码中删除弃用?因此,我似乎收到了警告。删除线文本是“新通知”和 setLatestEventInfo?它说“此方法在 API 级别 11 中已弃用?这是什么意思? 请帮忙。我该如何解决?

@SuppressWarnings("deprecation")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "OnStartCommand()", Toast.LENGTH_SHORT).show();
    NotificationManager notificationmanager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    Intent notificationintent = new Intent(this, Reminder_2.class);
    PendingIntent pendingintent = PendingIntent.getActivity(this, 0, notificationintent, 0);
    int icon=R.drawable.ic_launcher;
    long when=System.currentTimeMillis();
    @SuppressWarnings("deprecation")
    Notification notification=new Notification (icon, "There's a message", when);
    notification.setLatestEventInfo(this, "Title here", "Content here.", pendingintent);
    notificationmanager.notify(033, notification);
    return super.onStartCommand(intent, flags, startId);
}

【问题讨论】:

    标签: android notifications deprecated suppress-warnings


    【解决方案1】:

    它是deprecated,这意味着它已被标记为在未来的 Android 版本中删除,或者已经引入了更好的标准/替代方案。文档建议改用 Notification.Builder:

    Notification noti = new Notification.Builder(mContext)
             .setContentTitle("New mail from " + sender.toString())
             .setContentText(subject)
             .setSmallIcon(R.drawable.new_mail)
             .setLargeIcon(aBitmap)
             .build();
    

    查看文档:http://developer.android.com/reference/android/app/Notification.Builder.html

    【讨论】:

      【解决方案2】:

      这是什么意思?

      在编写计算机软件、其标准或文档的过程中,弃用是一种应用于软件功能的状态,表明应避免使用这些功能,通常是因为它们已被取代。尽管软件中保留了已弃用的功能,但它们的使用可能会发出警告消息,推荐替代做法,并且弃用可能表明该功能将在未来被删除。功能已被弃用(而不是立即删除),以提供向后兼容性,并让使用该功能的程序员有时间使其代码符合新标准。

      来源:Here

      我该如何解决?

      您可以在documentation of Notification 中找到,

      Notification(int icon, CharSequence tickerText, long when)
      

      可以替换为Notification.Builder

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-27
        • 1970-01-01
        • 2016-06-24
        • 2022-07-20
        • 1970-01-01
        • 2014-12-22
        • 2011-04-05
        相关资源
        最近更新 更多