【问题标题】:My code uses or overrides a deprecated API我的代码使用或覆盖了已弃用的 API
【发布时间】:2021-09-04 17:53:05
【问题描述】:

我有一个代码可以运行在一个版本上,但适用于最新版本。 Android工作室写道: “使用或覆盖已弃用的 API。使用 -Xlint:deprecation 重新编译以了解详细信息。”

我的代码是:

 Notification.Builder builder = new Notification.Builder(this);
        builder.setContentTitle("Words reminder");
        builder.setContentText("It's time to try yourself!");
        builder.setSmallIcon(R.drawable.ic_stat_name);
        builder.setAutoCancel(true);
        builder.setPriority(Notification.PRIORITY_HIGH);
        //builder.build().flags |= Notification.FLAG_AUTO_CANCEL;
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        builder.setSound(alarmSound);

        Intent notifyIntent = new Intent(this, RightOrNot.class);
        notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
        Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, 0);

        builder.setContentIntent(pendingIntent);
        Notification notificationCompat = builder.build();
        NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
        managerCompat.notify(NOTIFICATION_ID, notificationCompat);

我相信问题出在第一行:

Notification.Builder builder = new Notification.Builder(this);

您知道如何修复线路或使用适用于所有版本的不同方法吗?

【问题讨论】:

  • 这只是一个警告。你范忽略它。
  • 但是在其他使用最新版本的手机上不行
  • 你必须为oreo及以上版本创建一个通知渠道。

标签: java android notifications


【解决方案1】:

根据 android 开发者文档,public Builder(Context context) 已被弃用,您应该尝试改用 public Builder(Context context, String channelID)。您需要使用通知渠道。按照步骤here.

【讨论】:

  • 谢谢你,但它没有帮助,同样的警告,仍然无法向最新版本的手机发送通知(
  • 为什么你接受了一个你说没有帮助的答案?
  • 问题必须出在您的构建器函数调用中,因为 api 文档说构建器函数已被弃用。你现在遇到什么问题?问题出在哪里?
猜你喜欢
  • 1970-01-01
  • 2012-03-04
  • 2021-12-11
  • 2020-04-19
  • 2021-04-27
  • 2021-08-26
  • 2021-12-20
  • 1970-01-01
相关资源
最近更新 更多