I need to set an alarm on a user's phone. This is an android function, and the alarm takes the time to fire in System.currentTimeMillis() format. I want to set the alarm at 7pm on the user's phone, but I don't know what timezone the user has their phone set to.
How can I figure out what the equivalent time at 7pm for the current day is in UTC (currentTimeMillis()) ?
Thanks
Calendar cal = Calendar.getInstance(); // returns current time in user's default TimeZone
cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.HOUR_OF_DAY, 19);
long time = cal.getTime().getTime();
Related
I'm trying to make an alarm that besides shoot today, repeat on the exact same hour on the next day.
Not having any success until now.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, timeHOUR);
calendar.set(Calendar.HOUR, timeHOUR);
calendar.set(Calendar.MINUTE, timeMINUTE);
The alarm must occur at the same times on the following days.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Also i need to be able to register more than 1 alarm. I'm not sure if setRepeating() supports that.
i made a calendar to show toast in specific time every day at 8:00 pm
but the app show start the service and show the toast in different time.
like
8:00 pm
8:23 pm
8:30 pm
8:32 pm
8:50 pm
and doesn't stop showing the toast
what is the wrong?
this is my Mainactivity.java code
Intent alarmIntent = new Intent(MainActivity.this, MyService.class);
pendingIntent = PendingIntent.getService(MainActivity.this, 0, alarmIntent, 0);
setAlarm();
}
private void setAlarm() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
/* Set the alarm to start at 8.00 PM */
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 20);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
//Add a day if alarm is set for before current time, so the alarm is triggered the next day
if (calendar.before(Calendar.getInstance())) {
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS), pendingIntent);
}}
From the documentation of setRepeating
Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.
EDIT
You alarm being inexact, it means that the system will not wake up in the exact time you set 00:800 to fulfill your request, instead it will group some actions that needs to be done, somewhere around the time you set, and fire them all together to save battery of the phone, and do prevent the phone from being awake every couple of seconds separately.
You can replace your call to non repeating call using AlarmManager.set() and handling repetition yourself, as in each alarm firing schedules the next one.
Or if your alarm firing is not time critical (it doesn't not have to be very exact), you can leave it as it is.
To have a precise alarm instead of using setRepeating use setAndAllowWhileIdle
otherwise, if you are not in need of precise timing I would recommend you to use firebase job dispatcher to schedule your job https://github.com/firebase/firebase-jobdispatcher-android
I'm looking for some ways to make an app that every body can use all the futures for just 3 days, after the limited time ends some of futures disables but again via In-App Purchase they will ready to use.
The question is, How can I set a timer to count down for 3 days from the time that application for first time opened? I should do it in some way that when the phone power offs there mustn't be problem.again after powering on and without opening the app it should count down to 00:00:00.
Is there any samples for this? Or how can I do it?
As #4castle said, set a time stamp when they first open the app. Then, if you want to display a countdown with an exact time until expiration, have a text box somewhere that displays time remaining. Calendar
import java.util.Calendar
if( this.firstTimeRunning() ){
Calendar start = Calendar.getInstance();
//save start.getTimeInMillis()
}
Calendar end = Calendar.getInstance();
end.setTimeInMillis( /* saved millisec value */);
end.set(Calendar.DAY, end.get(Calendar.DAY) + 3);
long difference = end.getTimeInMillis() - start.getTimeInMillis();
text_box.setText(/* format difference*/); //update every second
Something like that.
I am developing an android app where I am trying to set an alarm on daily basis.And for that I am using Calendar as
alarmCalendar.set(Calendar.DATE,taskdate);
alarmCalendar.set(Calendar.MONTH, 7);
alarmCalendar.set(Calendar.YEAR, 2013);
alarmCalendar.set(Calendar.HOUR, 11);
alarmCalendar.set(Calendar.MINUTE, 30);
alarmCalendar.set(Calendar.SECOND, 0);
I was setting the alarm using the below code.
alarmtime = alarmCalendar.getTimeInMillis();
am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, AlarmManager.INTERVAL_DAY, pi);
The Problem with the above code is, the alarm triggers sometimes and sometimes it doesn't.
So I am trying it out in one more way. that is,
Once the alarm triggers, On cancel of that alarm I am trying to set it for the next day using
am.set(AlarmManager.RTC_WAKEUP, currenttime, pendingIntent);
instead of am.setRepeating(). But now the problem is that, even though there is no looping in the code, the alarm triggers continuously on cancelling it.
Not getting where I am going wrong.
Please Help.Thanks!
Do you have a BroadcastReceiver with the BOOT_COMPLETED permission to recreate the alarm every time the phone starts up? Otherwise you'll loose the alarm.
Check it out here http://learnandroideasily.blogspot.nl/2013/07/bootcompleted-broadcastreceiver-in.html
How can i synchronize the time (second , hour).
i have this
int minuto = cal.get(Calendar.MINUTE);
int day_Completed = 1440;
but i have no idea how can i do it.
I tried doing this
changing the pc time when a loop is running to see if the var minuto change.
but doesn't work.
An instance of calendar statically reflects one moment in time. It won't get updated automatically, it is not behaving like a clock.
If you want to "synchronize" with the actual time, then you have to
use some sort of timer, maybe based on Thread.sleep for a start
on each "timer event" get the current time (System.currentTimMillis()) and
update the instance of calendar with that value.