通知管理器中的 "id" 值可以有多长.notify()



我正在尝试将唯一值传递给notificationManager.notify()。我脑海中浮现的最短方式是System.currentTimeMillis()

所以我做了以下以获得独特的价值。

 Long getCurrentTime= System.currentTimeMillis();
 int id= Math.abs(getCurrentTime.intValue());
 notificationManager.notify("com.myapp.app",id , notification);

System.currentTimeMillis()返回 13 位long值。

我有点害怕这个生成的id的长度.

我想知道notificationManager.notify()方法中id的长度是多少。

为什么不在主活动中创建一个静态计数器,并在呼叫通知之前递增它

主要活动

public static int NOTIFY_COUNTER_ID= 1;

通知类

notificationManager.notify("com.myapp.app",++MainActivity.NOTIFY_COUNTER_ID, notification);

编辑

正如您在评论中提到的,该应用程序可能会被杀死,因此您将无法访问静态资源。

为了避免这种情况,您可以使用共享首选项来保存增量 ID。

或者恕我直言,如果你使用Math.rand((有很大的"差距",你应该没问题。

System.currentTimeMillis()太长,无法管理标识符。Id 是 int,它在 java 中的 32 位,建议值为 -2,147,483,648 到 +2,147,483,647

最新更新