android - AlarmManager executes my PendingIntent immediately instead of waiting for sertain time -
i trying implement alarm, , therefore app has 2 activities: main 1 (where user sets alarm) , 'ringing' 1 displayed when alarm triggered. here how send intent alarmmanager call 'ringing' window:
public void schedulealarm(view view) { intent intent = new intent(this, wakeup.class); intent.setaction(intent.action_view); intent.setflags(intent.flag_activity_reorder_to_front); pendingintent wakeup = pendingintent.getactivity(this, 123, intent, 0); alarmmanager alarmmanager = (alarmmanager) this.getsystemservice(this.alarm_service); alarmmanager.setexact(alarmmanager.rtc_wakeup, calculatemillis(), wakeup); // setexact pretty made setting alarms anyway } private long calculatemillis() { calendar cal = calendar.getinstance(); cal.set(calendar.hour, hour); cal.set(calendar.minute, minute); cal.set(calendar.second, 0); return cal.gettimeinmillis() - calendar.getinstance().gettimeinmillis(); }
in manifest have following:
<application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".wakeupwithmath" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".wakeup" android:configchanges="orientation|keyboardhidden|screensize" android:label="@string/title_activity_wake_up" android:theme="@style/fullscreentheme" > <intent-filter> <action android:name="android.intent.action.view" /> </intent-filter> </activity> </application>
i have tested multiple times , different values calculatemillis (even mocked return 30000) yet result same: right after calling alarmmanager.setexact
it displayes 'ringing' activity , not wait time pass @ first.
what have missed? manifest configured wrong? need use <receiver>
tag? if so, how should configure it?
setexact
uses absolute time, not relative time. use time returned cal.gettimeinmillis()
without subtracting current time.