마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

AlarmManager 현재시간에 바로 호출하는 방법

0 추천

알람매니저를 현 시간부터 계속 interval 시간마다 호출하기 위해

 

AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
 
        Intent intent = new Intent(context, WifiAlarmService.class);
PendingIntent operation = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        
        alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(), interval, operation);
 
이렇게 구현하였습니다.
System.currentTimeMillis() 를 하면 현재시간이니 바로 알람에 등록한 서비스가 깨어날거라 생각했는데
 
서비스가 깨어나질 않네요 .. adb로 dumpsys 해보면 알람이 등록은 돼 있는데 말입니다.
 
현재 시간부터 바로 알람을 실행하고싶은데 어떻게 하면 될지 도움좀 부탁드립니다.
음무 (17,820 포인트) 님이 2015년 2월 24일 질문

1개의 답변

+1 추천
 
채택된 답변
System.currentTimeMillis() 를 그대로 사용하면 당연히 알람이 실행되지 않습니다.

System.currentTimeMillis() 보다 1~2초 정도 이후 시간으로 등록해보세요.
익명사용자 님이 2015년 2월 24일 답변
음무님이 2015년 2월 24일 채택됨
말씀해주신데로 적용하여

        AlarmManager alarmManager = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
       
        Intent intent = new Intent(MainActivity.this, WifiAlarmService.class);
        PendingIntent operation = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        
        alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis() + 2000, 3000, operation);

이렇게 했는데 정작 WifiAlarmService가 호출되지않습니다 ㅠㅠ 원인을 모르겠네요 ..
매니패스트에 <service
            android:name=".WifiAlarmService" /> 선언도 돼있구요 ...
앗 해결됐습니다. 알려주신데로 수정 했는데도 안됐던 이유는 서비스에서 생성자 오류가 있었습니다. 감사합니다 !
...