Android每天执行几次接收器

时间:2014-01-25 21:08:02

标签: android broadcastreceiver broadcast

更确切地说,我想做以下事情:AlarmReceiver-Broadcaster应该每天执行两次:TimerTask仍然是更好的解决方案吗?

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 18);
    calendar.set(Calendar.MINUTE, 54);

    Intent intentAlarm = new Intent(context, AlarmReceiver.class);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            PendingIntent.getBroadcast(context, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 17);
    calendar.set(Calendar.MINUTE, 58);

    Intent intentAlarm2 = new Intent(context, AlarmReceiver.class);
    AlarmManager alarmManager2 = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager2.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            PendingIntent.getBroadcast(context, 1, intentAlarm2, PendingIntent.FLAG_UPDATE_CURRENT));

1 个答案:

答案 0 :(得分:0)

我认为你要找的是TimerTask in Android。以下是如何使用example的示例。