通知计数不计算在内

时间:2012-05-22 12:48:07

标签: 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);
}

2 个答案:

答案 0 :(得分:3)

您将计数设为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);
}

答案 1 :(得分:1)

int count=1;全局设为活动,因为您声明了NOTIFY_ME_ID。您在givenotification()内声明计数,因此它始终以1初始化。