【问题标题】:How to set multiple alarms firing distinct notifications for each in Android如何在Android中设置多个警报为每个警报触发不同的通知
【发布时间】:2021-08-24 19:05:16
【问题描述】:

我正在尝试开发一个应用程序,在该应用程序中,用户可以同时创建多个警报,例如 09:48。而这一次,每个警报都必须创建自己的通知,当点击通知时将用户引导到一个活动。在同一时间安排的其他警报必须保持响铃,直到他们的通知被点击。现在,我可以为不同的时间创建警报并获取相应的通知,从而导致用户可以关闭或暂停警报的活动。但是,当我同时安排多个闹钟时,只有一个闹钟在响,另一个闹钟会丢失。我在下面分享我的代码片段。

public void schedule(Context context) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    Intent intent = new Intent(context, AlarmBroadcastReceiver.class);


    PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(context, alarmId, intent, 0);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    alarmManager.setExact(
            AlarmManager.RTC_WAKEUP,
            calendar.getTimeInMillis(),
            alarmPendingIntent
    );

在我的 broadcastReceiver 类中:

    private void startAlarmService(Context context, Intent intent) {
    Intent intentService = new Intent(context, AlarmService.class);
    Log.println(Log.DEBUG, "DEBUG", "************Alarm Broadcast Receiver**********");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        context.startForegroundService(intentService);
    } else {
        context.startService(intentService);
    }
}

在我的 AlarmService 中:

    @Override
public void onCreate() {
    super.onCreate();

    mediaPlayer = MediaPlayer.create(this, R.raw.alarm);
    mediaPlayer.setLooping(true);

    vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Intent notificationIntent = new Intent(this, RingActivity.class);
    int requestCode = new Random().nextInt(Integer.MAX_VALUE);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, notificationIntent, 0);

    //String alarmTitle = String.format("%s Alarm", intent.getStringExtra(TITLE));

    int notificationId = new Random().nextInt(Integer.MAX_VALUE);
    Notification notification = new NotificationCompat.Builder(this, String.valueOf(notificationId))
            .setContentTitle("PASS1")
            .setContentText("PASS2")
            .setSmallIcon(R.drawable.pill)
            .setContentIntent(pendingIntent)
            .build();

    mediaPlayer.start();

    long[] pattern = { 0, 100, 1000 };
    vibrator.vibrate(pattern, 0);

    startForeground(1, notification);

    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();

    mediaPlayer.stop();
    vibrator.cancel();
}

【问题讨论】:

    标签: java android android-studio alarmmanager


    【解决方案1】:

    在这段代码中

    startForeground(1, notification);
    

    您使用 1 作为 id。

    如果您的应用程序已经发布了具有相同 id 的通知并且尚未取消,则它将被更新的信息替换。

    因此,具有相同 id 的第二个警报通知(因为您使用的是固定值 1)正在替换旧的警报通知

    【讨论】:

      猜你喜欢
      • 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
      相关资源
      最近更新 更多