4.1以下不支持通知生成器.我如何指定只使用4.1及以上版本的通知生成器,而忽略4.1以下版本



我在想如果语句为isJellyBeanOrHigher(),但不知道如何实现它。任何关于如何解决我的问题的建议都会很有帮助。

Notification notification = new Notification.Builder(this)
            .setContentTitle(
                    mContext.getString(R.string.ui_maps_display_notification_title))
            .setContentText(
                    getResources().getString(
                            R.string.ui_maps_display_notification_content))
            .setSmallIcon(R.drawable.ic_launcher).setContentIntent(pi)
            .build();
    mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notification.flags = notification.flags
            | Notification.FLAG_ONGOING_EVENT;
    mNotificationManager.notify(0, notification);
编辑:

 Notification notification = new NotificationCompat.Builder(this)
            .setContentTitle(
                    mContext.getString(R.string.ui_maps_display_notification_title))
            .setContentText(
                    getResources().getString(
                            R.string.ui_maps_display_notification_content))
            .setSmallIcon(R.drawable.ic_launcher).setContentIntent(pi)
            .build();
    mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notification.flags = notification.flags
            | Notification.FLAG_ONGOING_EVENT;
    mNotificationManager.notify(0, notification);

检查应用运行的操作系统版本:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    //do something
} else {
    //do something else
}

你还必须用@TargetApi()注释你的函数,以防止在被检查的块内调用方法时编译器发出警告。

我也同意Guardanis的观点,你应该试试NotificationCompat.Builder

检查android.os.Build.VERSION

  • CODENAME:当前的开发代码,或者字符串"REL",如果这是一个发布版本。
  • INCREMENTAL:底层源代码控件用来表示此构建的内部值。
  • RELEASE:用户可见的版本字符串

最新更新