简体   繁体   English

服务启动后而非设定时间触发警报

[英]Alarm fires after service start instead of set time

I'm trying to set a trigger for 5 AM every morning to get the phone ready for a USER_PRESENT broadcast, but the trigger goes off as soon as the service is started. 我正在尝试将触发器设置为每天早上5点,以准备好进行USER_PRESENT广播的电话,但是一旦服务启动,触发器就会关闭。 The service (PAService) is turned off and on by a switch on the main activity using stopService and startService . 服务(PAService)通过使用stopServicestartService在主要活动上的开关来关闭和打开。 This code works fine in the emulator but not an actual Android phone. 此代码在模拟器中可以正常运行,但在实际的Android手机中则无法正常工作。

public class PAService extends Service {

static boolean is_ready_to_speak = false;
PendingIntent pendingIntent;

public PAService() {

}

public class ScreenReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if(is_ready_to_speak)
        {
            PASpeak paspeak = new PASpeak();
            paspeak.sayit(getApplicationContext(),"You're using your phone for the first time this morning");
           is_ready_to_speak = false;
        }
    }
}


public static class AlarmReceiver extends BroadcastReceiver {

    public AlarmReceiver()
    {
        Log.d("AlarmReceiver func called","alarm receiver func called");
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("RECEIVED BROADCAST", "Sometype of ALARM Broadcast received");
        PASpeak paspeak = new PASpeak();
        paspeak.sayit(context.getApplicationContext(),"ALARM ALARM ALARM");
        is_ready_to_speak = true;
    }

}



@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    IntentFilter filter = new IntentFilter(Intent.ACTION_USER_PRESENT);
    BroadcastReceiver mReceiver = new ScreenReceiver();
    registerReceiver(mReceiver, filter);

    PASpeak paspeak = new PASpeak();
    paspeak.sayit(getApplicationContext(),"process has started");

    Intent alarmIntent = new Intent(PAService.this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(PAService.this, 1, alarmIntent, 0);

    // Set the alarm to start at 5:00 AM
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 5);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);

    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    manager.setRepeating(AlarmManager.RTC_WAKEUP,
            calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, // for repeating
            // in every 24
            // hours
            pendingIntent);

    return Service.START_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

} }

manager.setRepeating(AlarmManager.RTC_WAKEUP,
            calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, // for repeating
            // in every 24
            // hours
            pendingIntent);

The 2nd parameter , trigger time triggers the alarm immediately if calendar.getTimeInMillis() is in the past. 如果calendar.getTimeInMillis()过去,则2nd parameter (触发时间calendar.getTimeInMillis()立即触发警报。 So what's happening is you are opening the app maybe at 7PM. 所以发生的事情是您可能在晚上7点打开应用程序。 You expect that the alarm goes off 5AM next morning, but your calendar.getTimeInMillis() would have 5AM the same day morning. 您预计警报将在第二天早上5点发出,但您的calendar.getTimeInMillis()将在当天早上5点发出。 So you need to add a check for this: 因此,您需要为此添加检查:

Calendar calendar = Calendar.getInstance();
// calendar.setTimeInMillis(System.currentTimeMillis()); // YOU DON'T NEED THIS LINE
calendar.set(Calendar.HOUR_OF_DAY, 5);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);

Calendar current = Calendar.getInstance();

int curTime = current.getTimeInMillis();
int alarmTime = calendar.getTimeInMillis();

if (alarmTime >= curTime){
   manager.setRepeating(AlarmManager.RTC_WAKEUP,
        alarmTime, AlarmManager.INTERVAL_DAY, // for repeating
        // in every 24
        // hours
        pendingIntent);
}else{
   calendar.add(Calendar.DAY_OF_MONTH, 1);
   int alarmTime = calendar.getTimeInMillis();
   manager.setRepeating(AlarmManager.RTC_WAKEUP,
        alarmTime, AlarmManager.INTERVAL_DAY, // for repeating
        // in every 24
        // hours
        pendingIntent);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每次应用启动后执行警报管理器而不是设定时间 - Alarm manager executing after every app lauch instead of set period 如果时区不同于格林尼治标准时间(UTC),则AlarmManager在错误的时间触发警报 - AlarmManager fires alarm in the wrong time if timezone is different than GMT(UTC) Alarmmanager.set() 总是在应用程序启动而不是预期时间时触发 - Alarmmanager.set() always fires when application starts instead of expected time 服务在单播上触发数百次 - Service Fires Hundreds Of Time On Single Broadcast 即使重启设备后,如何在计划的时间使用挂起的意图环设置排队的警报? - How to set queued alarm using pending intent ring at scheduled time even after restarted the device? 当闹钟时间与实时时间相距1小时的倍数时,会触发PendingIntent - PendingIntent fires when the alarm time is a multiple of 1 hour away from real time - Android 警报管理器:重复警报并不总是触发 - Alarm manager: repeating alarm not always fires Android在Alarm Manager中设置特定的时间和日期 - Android set specific time and date in Alarm Manager 为Android设置基于数据库值/时间的警报? - Set an alarm based of a Database value/time for Android? Android:手机重启后设置闹钟/提醒 - Android: set alarm/reminder after phone reboot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM