8

こんにちは、すべての通知を 1 つのビューに表示したい..ステータス バーの通知数を更新したい...すべての情報を更新していますが、常に 1 を表示しています..解決方法を教えてください...

@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);
}
4

3 に答える 3

19

カウントを追跡する必要があります。Application クラスを拡張できます。

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 を変更する必要があります。

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

また、ユーザーが通知を開いたときにカウントをリセットできます。

AppName.setPendingNotificationsCount(0);
于 2013-02-17T03:41:18.433 に答える
4

これは私のコードで、動作します。私はあなたの古い Android バージョンでのみテストしました。新しいバージョンでは「数字」バッジが見えなくなっているのではないかと思いますが、テストする機会がありませんでした.

void notifyMsgReceived(String senderName, int count) {
    int titleResId;
    String expandedText, sender;

    // Get the sender for the ticker text
    // i.e. "Message from <sender name>"
    if (senderName != null && TextUtils.isGraphic(senderName)) {
        sender = senderName;
    }
    else {
        // Use "unknown" if the sender is unidentifiable.
        sender = getString(R.string.unknown);
    }

    // Display the first line of the notification:
    // 1 new message: call name
    // more than 1 new message: <number of messages> + " new messages"
    if (count == 1) {
        titleResId = R.string.notif_oneMsgReceivedTitle;
        expandedText = sender;
    }
    else {
        titleResId = R.string.notif_missedCallsTitle;
        expandedText = getString(R.string.notif_messagesReceivedTitle, count);
    }

    // Create the target intent
    final Intent intent = new Intent(this, TargetActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    final PendingIntent pendingIntent =
        PendingIntent.getActivity(this, REQUEST_CODE_MSG_RECEIVED, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    // Build the notification
    Notification notif = new Notification(
        R.drawable.ic_notif_msg_received,  // icon
        getString(R.string.notif_msgReceivedTicker, sender), // tickerText
        System.currentTimeMillis()); // when
        notif.setLatestEventInfo(this, getText(titleResId), expandedText, pendingIntent);
    notif.number = count;
    notif.flags = Notification.FLAG_AUTO_CANCEL;

    // Show the notification
    mNotificationMgr.notify(NOTIF_MSG_RECEIVED, notif);
}

後で通知を更新するのも簡単です。新しい値でメソッドを再度呼び出すだけです。通知が作成されたときに数字がゼロより大きい場合にのみ、数字が通知アイコン バッジに表示されます。

同様に、数値を 1 未満の数値に設定した場合、数値バッジは非表示になりません (数値は非表示になります)。再表示する前に通知をクリアすると、修正される可能性があります。

于 2012-10-09T16:16:37.707 に答える
0

カウントを追跡する必要があります。notif.number で実行しようとしているインクリメントは機能していません。その状態が利用できないためです (つまり、notif.number は常に 0 で、1 にインクリメントします)。アプリのどこかで番号を追跡し(おそらく共有設定)、インクリメントしてそこに保存し、通知を作成するときに設定します

notif.number = myStoredValueForWhatShouldShowInNumber;

試してみてください。

于 2012-11-19T19:13:32.873 に答える