Android在一天的两个不同时间运行相同的广播

时间:2015-11-06 15:23:27

标签: android alarmmanager

我想在一天中的两个不同时间运行我的服务,例如每天上午9点40分和下午3点30分。我尝试使用以下代码构建但它只运行我设置的最后一个警报,而不是两个警报。请帮我解释一下代码。先感谢您。我尝试过类似的问题,但它仍然没有给我一个好的结果。

**// set the alarm manager**
        this.context = this;

        Calendar updateTime = Calendar.getInstance();
        updateTime.set(Calendar.HOUR_OF_DAY, 20);
        updateTime.set(Calendar.MINUTE, 47);

        Calendar updateTime2 = Calendar.getInstance();
        updateTime2.set(Calendar.HOUR_OF_DAY, 20);
        updateTime2.set(Calendar.MINUTE, 49);

        Intent alarm = new Intent(MainActivity.this, AlarmReceiver.class);
        Intent alarm2 = new Intent(MainActivity.this, AlarmReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarm,
                PendingIntent.FLAG_ONE_SHOT);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC, updateTime.getTimeInMillis(), pendingIntent);
        PendingIntent pendingIntent2 = PendingIntent.getBroadcast(MainActivity.this, 0, alarm2,
                PendingIntent.FLAG_ONE_SHOT);

        AlarmManager alarmManager2 = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager2.set(AlarmManager.RTC, updateTime2.getTimeInMillis(), pendingIntent2);

3 个答案:

答案 0 :(得分:1)

你得到这个,因为第二个会覆盖第一个。例如,您可以通过为每个意图设置标识符来解决此问题。

答案 1 :(得分:1)

由于相同的意图,后者将覆盖前者。您可以为每个意图添加一个操作来分隔它们,如下所示

Intent alarm = new Intent(MainActivity.this, AlarmReceiver.class);
alarm.setAction("my.app.package.ACTION_ALARM_9_40AM");

Intent alarm2 = new Intent(MainActivity.this, AlarmReceiver.class);
alarm2.setAction("my.app.package.ACTION_ALARM_3_30PM");

我希望这对你有所帮助。

答案 2 :(得分:0)

我自己找到了答案。我使用了不同的ID,并且效果很好。

// set the alarm manager
        this.context = this;

        Calendar updateTime = Calendar.getInstance();
        updateTime.set(Calendar.HOUR_OF_DAY, 21);
        updateTime.set(Calendar.MINUTE, 16);

        Calendar updateTime2 = Calendar.getInstance();
        updateTime2.set(Calendar.HOUR_OF_DAY, 21);
        updateTime2.set(Calendar.MINUTE, 25);

        Intent alarm = new Intent(MainActivity.this, AlarmReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarm,
                PendingIntent.FLAG_ONE_SHOT);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC, updateTime.getTimeInMillis(), pendingIntent);


        Intent alarm2 = new Intent(MainActivity.this, AlarmReceiver.class);

        PendingIntent pendingIntent2 = PendingIntent.getBroadcast(MainActivity.this, 1, alarm2,
                PendingIntent.FLAG_ONE_SHOT);

        AlarmManager alarmManager2 = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager2.set(AlarmManager.RTC, updateTime2.getTimeInMillis(), pendingIntent2);

        lvCustomList = (ListView) findViewById(R.id.lv_custom_list);
        showList();