如何实现去$ P $通知的pcated方法 [英] How to implement the deprecated methods of Notification

查看:121
本文介绍了如何实现去$ P $通知的pcated方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,但不知道如何摆脱这一点。

I have a small problem but dont understand how to get out of this.

我创建了一个类提供通知,但这些线路都标德precated:

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
...

我可以写一个code是这样的:

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
    ...
}

我提供的最低SDK版本为8。

I providing the minimum sdk version as "8".

编辑:

我不喜欢如下:

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
        }

我可以写其他部分??

推荐答案

这就是我最终的解决方案:

This is how i ended up to the solution:

if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {

            notification = new Notification(icon, text, time);
            notification.setLatestEventInfo(this, title, text, contentIntent);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            mNM.notify(NOTIFICATION, notification);
        } else {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(
                    this);
            notification = builder.setContentIntent(contentIntent)
                    .setSmallIcon(icon).setTicker(text).setWhen(time)
                    .setAutoCancel(true).setContentTitle(title)
                    .setContentText(text).build();

            mNM.notify(NOTIFICATION, notification);
        }

编辑:

以上解决方案的工作。不过,因为 NotificationCompat.Builder 类介绍,我们可以跳过,如果条件检查比较当前API版本。所以,我们可以简单地删除如果...否则状态,去:

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 the if...else condition, and go with:

NotificationCompat.Builder builder = new NotificationCompat.Builder(
                        this);
notification = builder.setContentIntent(contentIntent)
                      .setSmallIcon(icon).setTicker(text).setWhen(time)
                      .setAutoCancel(true).setContentTitle(title)
                      .setContentText(text).build();
mNM.notify(NOTIFICATION, notification);

这篇关于如何实现去$ P $通知的pcated方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆