【问题标题】:The method setPriority (int) is undefined for the type Notification未为 Notification 类型定义方法 setPriority (int)
【发布时间】:2020-07-24 01:48:29
【问题描述】:

由于 Jelly Bean 可以设置通知的优先级。这样你甚至可以设置 PRIORITY_MIN 来隐藏状态栏上的通知图标。 我读过它,它很简单,你只需要使用它:

MyNotification.setPriority(Notification.PRIORITY_MIN);

我的问题是我收到编译错误: “Notification 类型的方法 setPriority (int) 未定义”

我的应用程序必须在不同版本的 Android 中运行,因此,我知道 JellyBean 下不提供该功能,因此有没有办法包含编译器标签或类似的东西来根据 Android 版本添加该功能。 我希望你明白我的意思。

【问题讨论】:

标签: android notifications


【解决方案1】:

Android 会自动处理这个问题。无需对特定版本号进行额外标记。当涉及到您的编译错误时,我很幸运能够使用下面的代码。您要确保使用 NotificationCompat 并确保您也导入了 android.support.v4.app.NotificationCompat;

这段代码应该可以工作:

int pri = 0;
NotificationCompat.Builder MyNotification = new NotificationCompat.Builder(ctx);
MyNotification.setPriority(pri);


【讨论】:

  • 谢谢。但对不起,我收到另一个编译错误:“NotificationCompat 无法解析为类型”
  • 确保将此添加到您的活动import android.support.v4.app.NotificationCompat;
  • 现在我收到错误“无法解析导入 android.support”:-(
  • 确保包含支持库。右击项目文件,下到Android Tools -> Add Support Library
【解决方案2】:

我只是想补充一点@EGHDK的回答。

这是很好的代码:

int pri = 0;
NotificationCompat.Builder MyNotification = new NotificationCompat.Builder(ctx);
MyNotification.setPriority(pri);

几乎没有改进:

NotificationCompat.Builder MyNotification = new NotificationCompat.Builder(ctx);
MyNotification.setPriority(NotificationCompat.PRIORITY_DEFAULT); // or any priority

不易出错的方法。因为我使用的是Notification.PRIORITY_DEFAULT,它必须NotificationCompat.PRIORITY_DEFAULT

任何可能出错的事情都会出错

Murphy's law

【讨论】:

    【解决方案3】:

    它不能工作超过 26 个 android API 所以检查条件:

    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
    val notificationBuilder:Notification.Builder=Notification.Builder(this,CHANNEL_Id)
                    .setContentText("Big Text Notification")
                    .setSmallIcon(R.drawable.ic_launcher_background)
                    .setLargeIcon(ic)
                    .setStyle(bigTextNotification)
                    .setContentText("this is Big Text Notification..")
                
    }
    else{
             .setContentText(" big text")
                    .setSmallIcon(R.drawable.ic_launcher_background)
                    .setLargeIcon(ic)
                    .setStyle(bigText)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
           
    }
    

    你可以使用NotificationCompat.PRIORITY_DEFAULT

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多