m taking time from time picker and when i set scheduling time to do work, it creating problem
SharedPreferences prefs = getSharedPreferences("CONSTANT_FILE_NAME",Context.MODE_WORLD_WRITEABLE);
SharedPreferences.Editor editor= prefs.edit();
editor.putLong("timepickerhour", timePicker.getCurrentHour());
editor.putLong("timepickerminute", timePicker.getCurrentMinute());
editor.putLong("milis", milis);
editor.commit();
SharedPreferences prfs = getSharedPreferences("CONSTANT_FILE_NAME",Context.MODE_PRIVATE);
h =prfs.getLong("timepickerhour",0);
m =prfs.getLong("timepickerminute",0);
r =prfs.getLong("milis",0);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent1 = new Intent(Scheduling.this, Feedback.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Scheduling.this, 0,
intent1, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.HOUR_OF_DAY, (int) h);
calendar1.set(Calendar.MINUTE, (int) m);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar1.getTimeInMillis(),
r, pendingIntent);
m facing problem when i give past time then now, as like now time is 4:25 pm if i set time 2:25 pm then that scheduling task run on the spot......
also problem if now time is 4:38 pm and i set time 4:38 am then also run code also now
plz help me
thnks in advance
same problem like this
How can i invoke the alarm for the past time using alarm manager in android?
if (calendar1.before(cal_now)) {// if its in the past increment
calendar1.add(Calendar.DATE, 1);
}
just add this line I think You will get right time:---
Calendar calendar1 = Calendar.getInstance();
calendar1.setTimeInMillis(System.currentTimeMillis());
Related
I am new to android ,here I am developing an alarm app for my working knowledge .
I have completed the following :
creating alarms and storing it into sqlite database.
Fetching all the alarms which has the status as active .
I have tried many stackoverflow post and their solutions and other blog posts which related to my doubt but I can't get a solution for my problem .
What is my problem is I am receiving number of alarm timings from sqlite database which I have set it before and I want to set all the alarms on the stored time .
Here I don't know how to set it .
Can anyone help me to set the multiple alarms .
I am really looking for someone's help to learn and experience these things please help me .
Thanks.
You need Alarm Manager and Pending Intent more.
for (int i = 0; i < ActivemyAlarms.size(); i++) {
int mHour = 0,mMin=0;
String amPm = null;
int mAlarmId = ActivemyAlarms.get(i).getALARM_ID(); //each alarm has an unique Id ,for differentiate one from another
String mAlarmTime = ActivemyAlarms.get(i).getALARM_TIME(); // alarm time (11:12:AM)
if (!(mAlarmTime == null)) {
String mtime = mAlarmTime; // alarm time format is 12hr format (ex : 11:12:AM)
String[] time = mtime.split(":");
mHour = Integer.parseInt(time[0].trim()); // get 11 hour
mMin = Integer.parseInt(time[1].trim()); // get 12 min
amPm = ((time[2].trim()));
}
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.HOUR_OF_DAY, amPm!=null && amPm.equalsIgnoreCase("pm")?(mHour+12):mHour);
calendar.set(calendar.MINUTE, mMin);
calendar.set(calendar.SECOND, 0);
calendar.set(calendar.MILLISECOND, 0);
Intent intent = new Intent(this,AlarmReceiver.class); //calling my Alarm service class which plays a music on the specific time
final int _id = (int) System.currentTimeMillis(); // get calendar instance
//Use Alarm manager and Pending Intent
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, mAlarmId, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
}
And to cancel any Alarm call alarmManager.cancel(PendingIntent) like;
Intent intent = new Intent(this,AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, mAlarmId, intent, 0);
alarmManager.cancel(alarmIntent);
I'm trying to write code that will run a task on specific hours every day.
I have an hours array that contains integers and I loop through it to set a repeating alarm from that hour and with an interval of a day.
If I just create the service and run the task every 10 seconds or something it does run the AlarmReciever but this code doesn't work after the addition of the Calendar API, what am I doing wrong?
AlarmManager alarmManager;
Calendar current = new GregorianCalendar();
Calendar calendar = new GregorianCalendar();
int[] hours = new int[] {14, 18, 21, 22};
public int onStartCommand(Intent intent, int flags, int startId) {
current.setTimeInMillis(System.currentTimeMillis());
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent1 = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent1, 0);
for (int hour : hours) {
calendar.add(Calendar.DAY_OF_YEAR, current.get(Calendar.DAY_OF_YEAR));
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.DATE, current.get(Calendar.DATE));
calendar.set(Calendar.MONTH, current.get(Calendar.MONTH));
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
return START_STICKY;
}
EDIT: I tried printing out calendar.getTimeInMillis() for every loop and here is the output
I/System.out: 2391249600000
I/System.out: 2643724800000
I/System.out: 2769962400000
I/System.out: 2833038000000
Lets say I take the two first numbers: 2643724800000 - 2391249600000 = 252475200000. 252475200000 / 1000 = 252475200. 252475200 / 60 = 4207920.
I'm pretty sure 4207920 minutes is more than one hour. Why is like this?
Okay, I found out how to fix my problem.
First of all thanks to #MikeL for suggesting to remove calendar.add(Calendar.DAY_OF_YEAR, current.get(Calendar.DAY_OF_YEAR)); because that did fix some stuff, but even after that it didn't work.
To fix the problem I changed PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent1, 0); to PendingIntent pendingIntent = PendingIntent.getBroadcast(this, hour, intent1, 0);
Basically changing the 0 to hour so that every pending intent is different.
Can you plese help me understand why my alarm won't fire :
I am trying to register a pending intent in oder to fire the alarm at 12:22 (current hour now) but the problem is that the alarm won't fire . I tried substituting the miliseconds from calendar.getTimeInMilies() with a hard-encoded time : 10 000 milies (10 seconds);
Not even that it had worked .
AlarmManager alarm_manager =(AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
calendar = Calendar.getInstance();
Intent intent = new Intent(MainActivity.this , AlarmManagerHelper.class);
PendingIntent register =PendingIntent.getActivity(getBaseContext(), 0, intent,0);
calendar.set(Calendar.YEAR,2014);
calendar.set(Calendar.MONTH, 10);
calendar.set(Calendar.DAY_OF_MONTH, 2);
calendar.set(Calendar.HOUR_OF_DAY,12);
calendar.set(Calendar.MINUTE,22);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
alarm_manager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), register);
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
Log.d(TAG ,"Time format is :" +formatter.format(calendar.getTime()));
I also have the following permission set in the Manifest
android:name="android.permission.WAKE_LOCK"
And the output :
Time format is :02/11/2014 12:22:00
Try to use getBroadcast method instead of getActivity:
PendingIntent register =PendingIntent.getBroadcast(MainActivity.this, 0, intent,0);
Hope this helps.
So I'm trying to make an AlarmManager that's suppose to launch at a specific time. The problem is that when I set the time, tt doesn't work. I think the main problem is that after I have change the time on Calendar, in logs object value "time" shows ?
timeOff = Calendar.getInstance();
System.out.println(timeOff);
timeOff.add(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);
timeOff.set(Calendar.HOUR_OF_DAY, 16);
timeOff.set(Calendar.MINUTE, 30);
timeOff.set(Calendar.SECOND, 0);
System.out.println(timeOff);
System.out.println("TIME:: "+timeOff.getTimeInMillis());
Intent intent = new Intent(TabsActivity.this, Receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(TabsActivity.this, 1122, intent, 0);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(am.RTC_WAKEUP, timeOff.getTimeInMillis(),timeOff.getTimeInMillis()+ 1000,pendingIntent);// + 604800000L,pendingIntent);
Logs shows this:
Before change:
java.util.GregorianCalendar[time=1406899765159,areFieldsSet=true,lenient=true,zone=Europe/Riga,firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=7,WEEK_OF_YEAR=31,WEEK_OF_MONTH=1,DAY_OF_MONTH=1,DAY_OF_YEAR=213,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=4,HOUR_OF_DAY=16,MINUTE=29,SECOND=25,MILLISECOND=159,ZONE_OFFSET=7200000,DST_OFFSET=3600000]
After Change:
java.util.GregorianCalendar[time=?,areFieldsSet=false,lenient=true,zone=Europe/Riga,firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=7,WEEK_OF_YEAR=32,WEEK_OF_MONTH=2,DAY_OF_MONTH=7,DAY_OF_YEAR=219,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=4,HOUR_OF_DAY=16,MINUTE=30,SECOND=0,MILLISECOND=159,ZONE_OFFSET=7200000,DST_OFFSET=3600000]
As you see the value -
Time=?
So my question, what am I doing wrong, why does after I have set the time, it doesn't show. And even if I try to log out plain timeOff.getTimeInMillis() , it doesn't show anything at all.
Solution
Change timeOff.add(Calendar.DAY_OF_WEEK,Calendar.FRIDAY) to timeOff.set(Calendar.DAY_OF_WEEK,Calendar.FRIDAY)
since calendar.add() method is abstract , you can use like this
Calendar timeOff = Calendar.getInstance();
System.out.println(timeOff);
timeOff.add(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);
timeOff.getTime()
//System.out.println(timeOff.getTime());
timeOff.set(Calendar.HOUR_OF_DAY, 16);
timeOff.set(Calendar.MINUTE, 30);
timeOff.set(Calendar.SECOND, 0);
System.out.println(timeOff);
System.out.println("TIME:: "+timeOff.getTimeInMillis());
I am trying to create the Alarms in my application using AlarmManager.
I am able to set multiple alarms, but if DATE parameter is added to Calender, the alarms are not at all triggered. Following is my code
Intent intent = new Intent(this, OneShotAlarm.class);
/*Pass the task row ID as the Unique ID for Pending Intent*/
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), (int) rowid , intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
calendar.clear();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, mHour);
calendar.set(Calendar.MINUTE, mMinute);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
long timeSet = calendar.getTimeInMillis();
alarmManager.set(AlarmManager.RTC_WAKEUP, timeSet, pendingIntent);
If I add the Date parameters to Calender as
calendar.add(Calendar.DAY_OF_MONTH, mDay);
calendar.add(Calendar.MONTH, mMonth);
calendar.add(Calendar.YEAR, mYear);
The alarms are not triggered. I have to schedule a event at a future date. Please suggest what I am missing. Thanks for the help!!
P.S. I am taking the date and time from Date & Time dialog picker
I have implement AlarmManager many times, Following technique will help you.
calculate your alarm time in milliseconds for example you want to set alarm after 10 minutes then 10*60*1000 millisecond after current time.
Add your calculated time in current millisecond
Example
long currentTime = System.currentTimeMillis();
long fireTime = 10 * 60 * 1000;
Intent ucintent = new Intent(getApplicationContext(),TimeAlarmReceiver.class);
ucintent.putExtra("isAlarm", true);
PendingIntent mTimeSlot = PendingIntent.getBroadcast(getApplicationContext(), (int)fireTime , ucintent, PendingIntent.FLAG_ONE_SHOT);
alarmManager.set(AlarmManager.RTC_WAKEUP,currentTime+ fireTime, mTimeSlot);
Above example works perfect.
Thank You,
Ketan's answer is good but there is an error.
long currentTime = System.currentTimeMillis();
long fireTime = 10 * 60 * 1000;
Intent ucintent = new Intent(getApplicationContext(),TimeAlarmReceiver.class);
ucintent.putExtra("isAlarm", true);
PendingIntent mTimeSlot = PendingIntent.getBroadcast(getApplicationContext(), (int) requestCode, ucintent, PendingIntent.FLAG_ONE_SHOT);
alarmManager.set(AlarmManager.RTC_WAKEUP,currentTime+ fireTime, mTimeSlot);
However you don't want "fireTime" in the PendingIntent. You should have a request code. Which is a code you create to identify your pending intents. It works in Ketan's case because he is always using the same time. But if you change the time, you will end up with two different intents.
see https://developer.android.com/reference/android/app/PendingIntent.html