0

I am thinking if statement for isJellyBeanOrHigher() but don't know how to implement it. Any suggestions on how to solve my issue would be very helpful. Below 4.1 it crashes the program.

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);

edit:

 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);
Eugene H
  • 3,520
  • 3
  • 22
  • 39
  • 1
    Why don't you just use the compatibility library? http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html – Cruceo Aug 06 '14 at 15:00
  • Agreed. `NotificationCompat.Builder` works back to API Level 4 and is *strongly* encouraged. – CommonsWare Aug 06 '14 at 15:05
  • I just changed Notification notification = new Notification.Builder(this) to Notification notification = new NotificationCompat.Builder.(this) I am assuming that this is the correct path. I need to set up the emulator this afternoon. I am at work sneaking in some code and can do it now. – Eugene H Aug 06 '14 at 15:15
  • I have it working on my phone 4.4. Changing it to NotificationCompat.Builder will solve the issue if I ran the app on a phone that is 4.0.3? – Eugene H Aug 06 '14 at 15:19
  • I am dying to test it but cant at this time. – Eugene H Aug 06 '14 at 15:20
  • @Guardanis if that works when I get home, write your answer below so I can accept it. – Eugene H Aug 06 '14 at 15:24

2 Answers2

0

Check against android.os.Build.VERSION.

  • CODENAME: The current development codename, or the string "REL" if this is a release build.
  • INCREMENTAL: The internal value used by the underlying source control to represent this build.
  • RELEASE: The user-visible version string.
Max McKinney
  • 1,218
  • 15
  • 25
0

Check what version of the OS your app is running on:

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

You'll also have to annotate your function with @TargetApi() to prevent compiler warnings when calling a method within the checked block.

I also agree with Guardanis, you should try out NotificationCompat.Builder

griffinjm
  • 493
  • 2
  • 10