Android-如何更新通知号 [英] Android - How to update notification number

查看:64
本文介绍了Android-如何更新通知号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个视图中显示所有通知..并想在状态栏中更新通知的数量...其更新所有信息,但始终显示数量1 ..请告诉我如何解决..

Hi i want to show all the notification in a single view .. and want to update number of notification in status bar ... its updating all info but showing number always 1.. please tell me how to solve it...

@Override
public void onReceive(Context context, Intent intent)
{
    //Random randGen = new Random();
    //int notify_id = randGen.nextInt();
    NotificationManager notificationManager = (NotificationManager)
        context.getSystemService(Activity.NOTIFICATION_SERVICE);
    String title = intent.getStringExtra(TableUtils.KEY_TITLE);
    String occasion = intent.getStringExtra(TableUtils.KEY_OCCASION);
    Notification notification = 
        new Notification(R.drawable.icon, "Love Cardz" , 
                         System.currentTimeMillis());
    // notification.vibrate = new long[]{100,250,300,330,390,420,500};
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.number+=1;
    Intent intent1 = new Intent(context, ThemesBrowserActivity.class);
    PendingIntent activity = 
        PendingIntent.getActivity(context, 1 , intent1, 
                                  PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(context, occasion, title, activity);
    notificationManager.notify(1, notification);
}

推荐答案

您必须跟踪计数.您可以扩展Application类:

You have to keep track of the count. You could extend the Application class:

public class AppName extends Application {
    private static int pendingNotificationsCount = 0;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    public static int getPendingNotificationsCount() {
        return pendingNotificationsCount;
    }

    public static void setPendingNotificationsCount(int pendingNotifications) {
        pendingNotificationsCount = pendingNotifications;
    }
}

您应该修改onReceive:

And you should modify the onReceive:

@Override
public void onReceive(Context context, Intent intent) {
    ...
    int pendingNotificationsCount = AppName.getPendingNotificationsCount() + 1;
    AppName.setPendingNotificationsCount(pendingNotificationsCount);
    notification.number = pendingNotificationsCount;
    ...
}

您可以在用户打开通知时重置计数:

And you could reset the count when the user open the notification:

AppName.setPendingNotificationsCount(0);

这篇关于Android-如何更新通知号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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