I have a small problem but dont understand how to get out of this.
I created a class for providing Notifications, but these lines are marked deprecated:
...
Notification notification = new Notification(icon, text, time); // deprecated in API level 11
...
notification.setLatestEventInfo(this, title, text, contentIntent); // deprecated in API level 11
...
Alternative methods are:
...
Notification noti = new Notification.Builder(mContext)
.setContentTitle("New mail from " + sender.toString())
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail)
.setLargeIcon(aBitmap)
.build(); // available from API level 11 and onwards
...
Can i write a code something like:
if(API_level < 11)
{
...
Notification notification = new Notification(icon, text, time); // deprecated in API level 11
...
notification.setLatestEventInfo(this, title, text, contentIntent); // deprecated in API level 11
...
}
else
{
...
Notification noti = new Notification.Builder(mContext)
.setContentTitle("New mail from " + sender.toString())
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail)
.setLargeIcon(aBitmap)
.build(); // available from API level 11 and onwards
...
}
I providing the minimum sdk version as "8".
Edit:
I did like below:
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB){
Notification notification = new Notification(icon, text, time);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TaskDetails.class), 0);
notification.setLatestEventInfo(this, title, text, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNM.notify(NOTIFICATION, notification);
}
else
{
// what to write here
}
What can i write for else
portion ??
This is how i ended up to the solution:
Edit:
The above solution works. Still, since,
NotificationCompat.Builder
class was introduced, we can skip the if condition for checking that compares current API version. So, we can simply remove theif...else
condition, and go with:This is the correct way to get the level.
All the version codes can be found at this developer link.