Browse - Programming Tips - Call deprecated method (when running on old Android level)

Date: 2019oct15
OS: Android
Language: Java

Q.  Call deprecated method (when running on old Android level)

A.
Intent notificationIntent = new Intent(this, Main.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); Notification notification; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { notification = new Notification(); notification.icon = icon; try { Method deprecatedMethod = notification.getClass().getMethod("setLatestEventInfo", Context.class, CharSequence.class, CharSequence.class, PendingIntent.class); deprecatedMethod.invoke(notification, context, strTitle, strBody, pendingIntent); } catch (Exception ex) { Log.w(Main.LOG_TAG, "Method setLatestEventInfo not found", ex); } } else { // Use new API Notification.Builder builder = new Notification.Builder(context) .setContentIntent(pendingIntent) .setSmallIcon(icon) .setContentTitle(strTitle); notification = builder.build(); } return notification;
From https://stackoverflow.com/questions/32345768/cannot-resolve-method-setlatesteventinfo