我在这里尝试做的是在每次弹出通知时将其内容随机化。但是我不知道如何实现,这就是我当前的代码:
Main2Activity
public void startAlarm() {
AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent myIntent;
PendingIntent pendingIntent;
myIntent = new Intent(Main2Activity.this,AlarmNotificationReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this,0,myIntent,0);
manager.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime()+0,86400000,pendingIntent);
}
这是我的接收器类:
AlarmNotificationReceiver
public class AlarmNotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder builder1 = new NotificationCompat.Builder(context);
NotificationCompat.Builder builder2 = new NotificationCompat.Builder(context);
NotificationCompat.Builder builder3 = new NotificationCompat.Builder(context);
builder1.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Alarm 1 actived!")
.setContentText("THIS IS MY ALARM")
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
.setContentInfo("Info");
builder2.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Alarm 2 actived!")
.setContentText("THIS IS MY ALARM")
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
.setContentInfo("Info");
builder3.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Alarm 3 actived!")
.setContentText("THIS IS MY ALARM")
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
.setContentInfo("Info");
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,builder1.build());
}
}
我的问题是在底部...
如何在每次触发警报时将其随机设置?
答案 0 :(得分:0)
使用Random
类生成随机对象(布尔值,整数等),然后使用swtich
生成生成的数字并使用适当的通知进行通知。
Random random = new Random(System.currentTimeMillis());
switch(random.nextInt(3)){
case 0:
// notify builder 1
break;
case 1:
// notify builder 2
break;
case 2:
// notify builder 3
break;
}
我真的建议您对代码进行重构,因为无需创建3个具有许多公共属性的相同对象,在这些对象中,您只能使用一个对象,并根据从随机生成器获得的情况更改其内容。下面是一个示例
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND);
Random random = new Random(System.currentTimeMillis());
switch (random.nextInt(3)) {
case 0:
builder.setContentTitle("Alarm 1 activated!")
.setContentText("THIS IS MY ALARM")
.setContentInfo("Info");
break;
case 1:
builder.setContentTitle("Alarm 2 activated!")
.setContentText("THIS IS MY ALARM")
.setContentInfo("Info");
break;
case 2:
builder.setContentTitle("Alarm 3 activated!")
.setContentText("THIS IS MY ALARM")
.setContentInfo("Info");
break;
}
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());