【问题标题】:Making a daily repeating notification but with randomized content each time每天重复通知,但每次都带有随机内容
【发布时间】:2018-09-15 16:53:18
【问题描述】:

我在这里尝试做的是在每次弹出通知时随机化通知的内容。但我不知道如何实现,这是我当前的代码:

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());
}
}

我的问题出在底部...

如何在每次触发警报时使其随机化?

【问题讨论】:

  • 什么样的随机化?你应该解释更多。
  • 每当调用onReceive() 时,我想要的随机化。在三个构建器(buider1buider2buider3)中,将调用一个构建器来创建通知。
  • 您可以生成随机数并根据您获得的数字触发通知生成器。
  • 已经完成了,idk 如果它在后台工作,因为它只显示builder1

标签: android android-alarms repeatingalarm


【解决方案1】:

使用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());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    相关资源
    最近更新 更多