警报不会被称为Android?

时间:2016-06-01 17:14:19

标签: android alarmmanager

我设置了2个警报,一个用于在60秒内发送通知,另一个用于调用服务来执行某项任务。

我希望这些警报重复,但我首先希望它们至少运行一次。我使用set和setExact(我希望它在那个时候完全运行)方法。

我有一个AlarmReceiver和一个wakefulbroadcastreceiver来识别调用了哪个警报并调用适当的服务。以下是我的代码:

在oncreate的主要活动中,我有:

 public void setApproxNotificationAlarm(){
        Log.d("ALARM", "create notification alarm");
        intent_notification = new Intent(this, AlarmReceiver.class);
        intent_notification.setAction(NOTIFICATION_ACTION);
        pi_notification = PendingIntent.getBroadcast(this, 111, intent_notification,0);
        alarm_notification = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarm_notification.set(alarm_notification.RTC_WAKEUP, System.currentTimeMillis() + 60 * 1000, pi_notification);
    }

    // Set Next day change alarm
    public void setExactNextDayChangeAlarm(){
        // At a time
        Log.d("ALARM", "create next day alarm");
        intent_next_day_change = new Intent(this, AlarmReceiver.class);
        intent_notification.setAction(NEXT_DAY_CHANGE_ACTION);
        pi_next_day_change = PendingIntent.getBroadcast(this, 100000003, intent_next_day_change,0);
        alarm_next_day_change = (AlarmManager) getSystemService(ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 19); // For 1 PM or 2 PM
        calendar.set(Calendar.MINUTE, 8);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            alarm_next_day_change.setExact(alarm_next_day_change.RTC_WAKEUP,calendar.getTimeInMillis(),pi_next_day_change);
        } else {
            alarm_next_day_change.set(alarm_next_day_change.RTC_WAKEUP, calendar.getTimeInMillis(), pi_next_day_change);
        }

    }

我在oncreate方法中调用它们。

这是我的警报接收者类:

public class AlarmReceiver extends WakefulBroadcastReceiver{

    public static String NOTIFICATION_ACTION = "com.example.ramapriyasridharan.alarm.notification";
    public static String NEXT_DAY_CHANGE_ACTION = "com.example.ramapriyasridharan.alarm.next.day.change";
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.d("ALARM", "action = "+action);
        if(NOTIFICATION_ACTION.equals(action)){
            Log.d("ALARM", "calling notitifcation service");
            startWakefulService(context, new Intent(context, NotificationService.class));

        }
        else if(NEXT_DAY_CHANGE_ACTION.equals(action)){
            Log.d("ALARM", "calling next day service");
            context.startService(new Intent(context, AlarmNextDayService.class));
            startWakefulService(context, new Intent(context, AlarmNextDayService.class));
        }
    }
}

我做错了吗?在任务完成后的服务中,我致电AlarmReceiver.completeWakefulIntent(intent);

欢迎任何建议,谢谢。

0 个答案:

没有答案