android - 通知计数未计数

标签 android count notifications notificationmanager

我有以下代码......当通知到来时......它总是显示“1”并且如果有更多通知则不计数......我做错了什么?

我将转到从以下位置开始的代码:

public class ActionSendSMS extends Activity {

private static final int NOTIFY_ME_ID=5476;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.actionsendsms);

    givenotification(getBaseContext());
    finish();
}

.... ....

public void givenotification(Context context){


    //Get the notification manager
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager nm = (NotificationManager)context.getSystemService(ns);

    //Create Notification Object
    int count=1;
    int icon = R.drawable.red_ball;
    CharSequence tickerText = "Nice message!";
    long when = System.currentTimeMillis();
    final Notification notify = new Notification(icon, tickerText, when);

    notify.flags |= Notification.DEFAULT_SOUND;
    notify.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
    notify.flags |= Notification.FLAG_AUTO_CANCEL;

    notify.number += count;

    //Set Notification send options
    Intent intent = new Intent(context, ActionNotifySendMessage.class);

    PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
    notify.setLatestEventInfo(context, "Message Alert", tickerText, pi);

    nm.notify(NOTIFY_ME_ID, notify);
}

最佳答案

您将计数设置为 count=1,然后将 notify.number 加 1 ??? 我从来没有看到你增加你的计数器本身......

您可以尝试将其设置为静态成员,并每次都增加它,如下所示:

public class ActionSendSMS extends Activity {

    private static final int NOTIFY_ME_ID=5476;
    private static int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actionsendsms);

        sendSMS();
        givenotification(getBaseContext());
        finish();
    }

    public void givenotification(Context context){


    //Get the notification manager
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager nm = (NotificationManager)context.getSystemService(ns);

    //Create Notification Object
    count++;
    int icon = R.drawable.red_ball;
    CharSequence tickerText = "PhoneFinder send GPS-message!";
    long when = System.currentTimeMillis();
    final Notification notify = new Notification(icon, tickerText, when);

    notify.flags |= Notification.DEFAULT_SOUND;
    notify.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
    notify.flags |= Notification.FLAG_AUTO_CANCEL;

    notify.number += count;

    //Set Notification send options
    Intent intent = new Intent(context, ActionNotifySendMessage.class);

    PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
    notify.setLatestEventInfo(context, "DroidFinder Alert", tickerText, pi);

    nm.notify(NOTIFY_ME_ID, notify);
}

关于android - 通知计数未计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10702349/

相关文章:

android - Android错误:权限拒绝:启动 Intent

SQL - 获取条件限制和总计数 - 是否需要子查询?

Mysql 到 MongoDB 以及与此模式等效的查询

具有多个页面的 Android Wear Smartwatch 通知 - 小图标仅显示在第一页上?

android - 从非 Activity 类创建通知

ios - 后台期间的 EAAccessory/EASession 拆卸

android - 使用导航架构时如何设置 `setReorderingAllowed`?

java - 我如何以编程方式发送图片作为电子邮件的一部分

google-sheets - 过滤不在谷歌表格列表中的数据的功能是什么?

java - 绕过 android-usb-主机-权限-确认-对话框