在 Android 中有时候我们调用
PendingIntent
想使用唯一的RequestCode
时, 可以使用时间戳生成。
博主博客
生成代码
private static final AtomicInteger requestCodeProvider = new AtomicInteger((int)SystemClock.elapsedRealtime());
public static int generatePendingIntentRequestCode() {
return requestCodeProvider.incrementAndGet();
}
public static void print() {
for (int i = 0; i < 6; i++) {
Log.e("nukix", "requestCode: " + generatePendingIntentRequestCode());
}
}
上面 print
方法调用后打印的结果为
requestCode: 13512170
requestCode: 13512171
requestCode: 13512172
requestCode: 13512173
requestCode: 13512174
requestCode: 13512175