【问题标题】:how to avoid clear notifications when activity finishes活动完成时如何避免清除通知
【发布时间】:2012-04-03 18:06:35
【问题描述】:

我正在向通知栏发布不同的通知。

所有人都有不同的 id,但意图是针对相同的活动。

当我点击第一个通知时,活动开始并点击结束。

当我的活动完成后,栏中所有剩余的通知都会消失。

我希望他们留在通知栏上。

如何做到这一点。

这是代码。

     mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                        int id = 10;
                        notificationIntent = new Intent(context, MyAct.class); // creating intent.
                        notificationIntent.putExtra("data", data);
                        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);         

                        // defining actions while notification.
                        contentIntent = PendingIntent.getActivity(context, id,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);               
                        notification = new Notification(R.drawable.icon, message,
                                System.currentTimeMillis());
                        notification.setLatestEventInfo(context, "Hello", message,
                                contentIntent);

                        notification.defaults |= Notification.DEFAULT_VIBRATE;
                        notification.defaults |= Notification.DEFAULT_LIGHTS;
                        notification.defaults |= Notification.DEFAULT_SOUND;
                        mNotificationManager.notify(id, notification);
                        id++;

提前谢谢....!

【问题讨论】:

    标签: android android-activity notifications


    【解决方案1】:

    您需要在清单文件中为该活动添加以下属性。

           android:launchMode="singleTask"
           android:taskAffinity=""
           android:excludeFromRecents="true"
    

    【讨论】:

      【解决方案2】:

      听起来您创建的通知不正确。我建议你重新阅读 Notification.Builder 类的 javadoc,看看你做错了什么。您可能忽略了调用 setAutoCancel(),或者您重新使用了相同的对象来发布通知。

      【讨论】:

      • 我正在根据id手动取消活动中的通知。
      【解决方案3】:

      您可以尝试使用正在进行的标志http://developer.android.com/reference/android/app/Notification.html#FLAG_ONGOING_EVENT 或在活动的 onNewIntent 方法的状态栏中添加通知

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多