I currently have a Fragment that has several Buttons and contains an onClickListener. Each time one of those buttons are clicked, a counter variable is incremented by 1, and is set as the text for a TextView in another Fragment, using SharedPreferences.
The counter will stay the same even after the app is completely closed, and will appear in subsequent runs of the app.
My new goal is to reset the counters back to 0 at the end of each day (23:59:00 for the time, to be exact).
I decided to avoid a Google search to figure this out, and found TimerTask, Calendar, Timer, and Date APIs on the Android Developer docs; I tried to get this to work with those APIs. Unfortunately it's not working out the way I planned. The variables are set back to 0, but they stay at zero and will only increment up to 1, and go back to 0 every time I exit the app.
Is there a better way to approach this? Or is my method sufficient, and I just need to adjust/change some of the code?
One of the problems might be where I'm changing the counter variable reference as well (and if so, where should I change it)?
Here is what I attempted:
FirstFragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflating the layout
View v = inflater.inflate(R.layout.starting_fragment, container, false);
//Instantiate new Timer
Timer timer = new Timer();
// Creates a Calendar object that specifies a specific time of day
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY, 20);
cal.set(Calendar.MINUTE, 57);
cal.set(Calendar.SECOND, 00);
cal.set(Calendar.MILLISECOND, 00);
// Instantiate a day object and use the time of day from cal object as its data
Date date = cal.getTime();
TimerTask tt = new TimerTask() {
// Sets the counter variables back to 0
#Override
public void run() {
COUNT_OOL = 0;
COUNT_WTE = 0;
COUNT_BLO = 0;
COUNT_BLK = 0;
COUNT_HBL = 0;
COUNT_GRN = 0;
COUNT_MTE = 0;
}
};
// Resets the counter variables (to 0) at the time specified by the date object
timer.schedule(tt, date);
// Stores count for each button back into their respective count variable
// Initializes the value from previous runs of app to subsequent runs of app
// This way, count variables will never get set back to 0 after onDestroy()
COUNT_OOL = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("oolongCount", 0);
COUNT_WTE = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("whiteCount", 0);
COUNT_BLO = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("bloomingCount", 0);
COUNT_BLK = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("blackCount", 0);
COUNT_HBL = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("herbalCount", 0);
COUNT_GRN = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("greenCount", 0);
COUNT_MTE = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("mateCount", 0);
The onClick method that increments the counter variables:
#Override
public void onClick(View view) {
int id = view.getId();
/*
* Use the View interface with OnClickListener to get the Button ID's
* Then you can run a switch on the Buttons (because normally switches
* cannot be run on buttons
*/
if (id == R.id.tea_type1) {
Builder oolongBuilder = new AlertDialog.Builder(StartingFragment.this.getActivity(),
AlertDialog.THEME_HOLO_LIGHT);
oolongBuilder.setPositiveButton("Hot",
//Starts OolongTeaActivity for hot tea when clicked
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
Intent i = new Intent(StartingFragment.this.getActivity(),
OolongTeaActivity.class);
StartingFragment.this.getActivity().startActivity(i);
}
});
oolongBuilder.setNeutralButton("Iced",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(StartingFragment.this.getActivity(),
ColdOolongTeaActivity.class);
StartingFragment.this.getActivity().startActivity(i);
}
});
oolongBuilder.setTitle("Oolong Tea");
oolongBuilder.setMessage("How Do You Like Your Tea?");
AlertDialog oolongDialog = oolongBuilder.create();
oolongDialog.show();
COUNT_OOL++;
SharedPreferences pref1 = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE);
SharedPreferences.Editor editor1 = pref1.edit();
editor1.putInt("oolongCount", COUNT_OOL);
editor1.commit();
}
SecondFragment (sets the counters as the text for TextViews):
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tea_counter, container, false);
oolongCounterText = (TextView) rootView.findViewById(R.id.oolong_counter_tv);
SharedPreferences pref1 = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE);
Integer counter1 = pref1.getInt("oolongCount", 0);
String s1 = String.valueOf(counter1);
oolongCounterText.setText(s1);
I would personally look at using the AlarmManager with the Calendar to set the time. You will then fire off a Service to do everything you need to do.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 0);
PendingIntent pi = PendingIntent.getService(context, 0,
new Intent(context, MyService.class),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);
Replace MyService with an actual Service, When the service starts it can:
1) Reset the number back to 0
2) Check if the app is running to see if you need to update the textboxes immediately or if it okay to wait for the user to launch the app
3) stop the service
Things to investigate before you follow this code:
Make sure the AlarmManager is right for you, a repeating alarm will NOT run after a reboot (Thanks to Jawnnypoo for clarifying this) Please see his comment below in which he links to a BroadcastReceiver so that the AlarmManager will run after a reboot.
Maybe just store the day of the year and compare with the current day of the year.
dayOfYear = DateFormat.format("D", new Date())
if (dayOfYear != lastResetDay) {
resetCounters();
}
Just save the last time the counter were reset in a pref.
When the fragment starts for the first time:
it checks the last time the counters were reset and maybe resets them
it registers an alarm via AlarmManager for the next midnight, which throws broadcast intent
it registers a broadcast receiver.
When the broadcast receiver receives the intent, it resets the counters, notifies the fragment and registers the alarm for the next midnight.
If the activity is stopped, remove your broadcast receiver and cancel your alarm, so that the app doesn't do work that wouldn't be visible anyway.
Using AlarmManager for this task seems tedious work. Just add another variable to your SharedPreferences. Store the last time the counter was updated.(Probably - Calendar.getInstance().getTimeInMillis())
So when you open the fragment while getting the counter value, you have to get last time it was updated. Check the stored time against your current day. If it doesn't matches then reset the counter value.
If I did miss anything let me know..:)
Some advises:
1.
COUNT_OOL = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("oolongCount", 0);
COUNT_WTE = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("whiteCount", 0);
COUNT_BLO = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("bloomingCount", 0);
COUNT_BLK = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("blackCount", 0);
COUNT_HBL = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("herbalCount", 0);
COUNT_GRN = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("greenCount", 0);
COUNT_MTE = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE).getInt("mateCount", 0);
should be edited like this
private SharedPreferences sharedPreferences;
sharedPreferences = getActivity().getSharedPreferences("keyname", Context.MODE_PRIVATE);
COUNT_OOL = sharedPreferences.getInt("oolongCount", 0);
etc
Named in accordance with the needs of specification
I think your code problem is to write more disorderly, finishing the first look at it, you first have a look, I now write about, by the way. Maybe you should use android system's clock.
Related
public static boolean setupAlarm(String flightName, long columnId, int time,int requestCode, Context context) {
AlarmManager alarmManager;
FlightTimeObject timeObject = DataCheckingUtils.getConvertedTime(time);
try {
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context.getApplicationContext(), ProcessAlarmReceiver.class);
intent.putExtra(IntentActions.INTENT_REQUEST_CODE, requestCode);
intent.putExtra(IntentActions.INTENT_SEND_STRING_FLIGHT, flightName);
intent.putExtra(IntentActions.INTENT_SEND_FLIGHT_COLUMN_ID, columnId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent,0);
//Get calendar instance
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//get hour
if (timeObject.getHour()!= -1) {
int hour = timeObject.getHour();
calendar.set(Calendar.HOUR_OF_DAY, hour);
}
//get minute
if (timeObject.getMinute()!=-1){
int minute = timeObject.getMinute();
calendar.set(Calendar.MINUTE, minute);
}
calendar.set(Calendar.SECOND, 0);
assert alarmManager != null;
alarmManager.setExact(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
pendingIntent);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
Hello everyone. The above code gets called within a for loop, underneath.
private static void setupAlarmToFlight(ArrayList<FlightObject> flightArray, Context context) {
int numberOfAlarms = 0;
int requestCode = 0;
for (int i = 0; i < flightArray.size(); i++){
FlightObject flight = flightArray.get(i);
String name = flight.getFlightName();
long flightColumnId = flight.getColumnId();
int flightActualTime = flight.getActualArrivalTime();
int scheduledTime = flight.getFlightScheduledTime();
int timeToParse = -2;
if (flightActualTime == -2){
timeToParse = scheduledTime;
}else{
timeToParse = flightActualTime;
}
boolean alarmSet = ExtractFlightUtilities.setupAlarm(
name,
flightColumnId,
timeToParse,
requestCode,
context);
if (alarmSet){
numberOfAlarms++;
requestCode++;
}
}
Intent intent = new Intent(IntentActions.ALARM_SET);
intent.putExtra(IntentActions.INTENT_SEND_INT, numberOfAlarms);
context.sendBroadcast(intent);
}
This code basically sets alarms for different flight arrival times from an arraylist that will start a service. The first alarm always fires right on time, but the rest never fire. I even stopped the service so it would just get to the receiver, but only the first fires. I also stopped the for loop at 2-3 alarms, but nothing.
I made sure that the hours and minutes are set correctly, and used another loop that would just set alarm in one minute after each but no luck.
Any help would be greatly appreciated.
EDITED:
I tried the suggestion and still not firing the alarms.
Something interesting I noticed, when setting breakpoints, the debugger gives preview values, and the first alarm that goes off okay, it's values, such as the requestCode and the Calendar values are green. All other following alarms are red.
For the first alarm.
The request code is green.
The pending intent looks all green.
The calendar value looks all green.
For all the other alarms.
The request code is red.
The pending intent request code red:
The calendar value is red
Again thank you for your time.
Since you set exact time, it might be because you did not check if the alarm time set is not passed already. E.g. consider the current date/time is 1-Jan-2018 11:30PM. If you set alarm for 01:00AM, based on your code, the alarm will be set for 1-Jan-2018 01:00AM since you used:
calendar.setTimeInMillis(System.currentTimeMillis())
which returns 1-Jan-2018 11:30PM and the you set time to 01:00AM which change the date to 1-Jan-2018 01:00AM which is passed. In this case you should add to alarm date one day. Something like this:
/**
* adjust time and date of alarm (alarms set in some previous years, will updated to today).
* if time is passed in today, date will +1
*
* #param alarmTimeCalendar time which may need adjustments
* #return adjusted time for alarm
*/
public static Calendar adjustAlarmTime(Calendar alarmTimeCalendar) {
Calendar adjustedDateCalendar = Calendar.getInstance();
int hour = alarmTimeCalendar.get(Calendar.HOUR_OF_DAY);
int minute = alarmTimeCalendar.get(Calendar.MINUTE);
adjustedDateCalendar.set(Calendar.HOUR_OF_DAY, hour);
adjustedDateCalendar.set(Calendar.MINUTE, minute);
adjustedDateCalendar.set(Calendar.SECOND, 0);
Date currentDate = adjustedDateCalendar.getTime();
Date alarmDate = adjustedDateCalendar.getTime();
long difference = alarmDate.getTime() - currentDate.getTime();
if (difference < 0) {
adjustedDateCalendar.add(Calendar.DATE, 1);
}
return adjustedDateCalendar;
}
So after trying multiple ways of approaching the problem, I solved it by setting the alarm as only RTC and not RTC_WAKEUP. Now all the alarms are firing right on time.
Thank you all.
When starting my app for the first time, i set up a Broadcast. The Broadcast is supposed to be repeating, but for test purposes i set it to setExact and call it everytime the app is started. The onReceive method does multiple things and it does them at the correct time. But the method within onReceive, which adds a new row to a Database is called immediatly when i register the Broadcast (before its triggered) and i cant figure out why. So everytime i start the app, it makes a new Entry and everytime onReceive gets called, it makes another (the correct) entry.
I call this method to set up the BroadcastReceiver when starting the app. I add +1 to Day if the time is in the past, so it then calls on the next day.
public void startDayCheckOnce() {
registerReceiver(new DayCheckReceiver(), new IntentFilter(DAY_CHECK_RECEIVER));
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
dayCheckIntent = new Intent(DAY_CHECK_RECEIVER);
sendBroadcast(dayCheckIntent);
dayCheckAlarmIntent = PendingIntent.getBroadcast(this, 2000000000, dayCheckIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 40);
if (calendar.before(Calendar.getInstance())) {
calendar.add(Calendar.DATE, 1);
}
//TODO: SET BACK TO SETREPEATING
alarmManager.setExact(AlarmManager.RTC, calendar.getTimeInMillis(), dayCheckAlarmIntent);
dayCheckStarted = true;
saveDayCheckStarted();
}
And this is the BroadcastReceiver. Everything gets called on the correct time, only
mDbHelper.addDayCheck()
gets called immediatly at the start of the app:
public class DayCheckReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (null != intent && DAY_CHECK_RECEIVER.equals(intent.getAction())) {
//load TaskList
SharedPreferences sharedPrefs = getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPrefs.getString(TASKLIST_FILE, null);
Type type = new TypeToken<ArrayList<TaskItem>>() {
}.getType();
mTaskList = gson.fromJson(json, type);
//modifications && writing to database
if (mTaskList != null && mTaskList.size() > 0) {
DatabaseHelper mDbHelper = new DatabaseHelper(context);
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -1);
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
String date = df.format(c.getTime());
String millisIdentifier;
int doneState;
for (int i = 0; i < mTaskList.size(); i++) {
millisIdentifier = mTaskList.get(i).getMilliIdentifier();
doneState = mTaskList.get(i).getDoneTodayValue();
mDbHelper.addDayCheck(millisIdentifier, date, doneState);
mTaskList.get(i).shiftDayChecks();
mTaskList.get(i).resetTimeLeft();
}
}
//save TaskList
SharedPreferences.Editor editor = sharedPrefs.edit();
gson = new Gson();
json = gson.toJson(mTaskList);
editor.putString(TASKLIST_FILE, json);
editor.apply();
//refresh Fragments
if (tasksFragment.isVisible()) {
FragmentTransaction refreshFT = getSupportFragmentManager().beginTransaction();
refreshFT.detach(tasksFragment);
refreshFT.attach(tasksFragment);
refreshFT.commit();
} else if (timerFragment.isVisible()) {
timerFragment.refreshUI();
}
}
}
}
And this is the dayCheck method itself:
public void addDayCheck(String millisIdentifier, String date, int doneState){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_DATE, date);
values.put(COLUMN_DONE_STATE, doneState);
db.insert(millisIdentifier, null, values);
}
EDIT:
Ok, the reason seems to be "sendBroadcast(dayCheckIntent)". I built that according to a tutorial and mustve done something wrong because this method apparently calls the broadcast instantly.
Ok i actually found the mistake. I called "sendBroadcast(dayCheckIntent) when setting up the Broadcast, and this called onReceive instantly. It seems like a simple mistake, but due to intersection if different other things, it seemed like onReceive was only called once, but actually that wasnt the case.
I am new with android and I wanna make a alarm app that gets a time from user and make android system to run application in that time.Now I calculate the time between current and the entered time and use the following code to sleep app but doesn't work when my app close or even minimized:
start_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TimePicker tp = (TimePicker) findViewById(R.id.timepicker1);
int hour = tp.getCurrentHour();
int min = tp.getCurrentMinute();
Integer res = min + hour*60;
Calendar c = Calendar.getInstance();
cur_hour = c.get(Calendar.HOUR_OF_DAY);
cur_min = c.get(Calendar.MINUTE);
Integer cur_res = cur_min + cur_hour*60;
if(cur_res > res)
{
res_min =1440-( cur_res - res);
}
else
{
res_min = res-cur_res;
}
res_min *= 60000;
new Handler().postDelayed(new Thread(){
#Override
public void run() {
super.run();
PlayMusic();
}
},res_min);
}
});
I searched hardly for solution and I find out that Alarm Manager is the answer but I couldn't find a proper code to work in this situation.I'm looking for a simple way to perform that.I don't know if there is a better way or not.
Thanks!
Did you try to use the AlarmManager? If you have, post the code that you've tried.
Look at the Google Developer API for classes such as these. They are fantastically documented.
http://developer.android.com/reference/android/app/AlarmManager.html
An example:
//You must set permissions in AndroidManifest.xml
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<receiver android:process=":remote" android:name="Alarm"></receiver>
//In your code
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), milli, sec, min, pi);
I have an application object declared in the manifest and this code runs when the application runs. What I want to accomplish is to set an alarm:
#Override
public void onCreate() {
super.onCreate();
singleton = this;
persister = new Persister();
am = (AlarmManager) getSystemService(ALARM_SERVICE);
sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
sharedPref.registerOnSharedPreferenceChangeListener(this);
registerAlarms();
}
public void registerAlarms() {
String sleepString = sharedPref.getString("time_sleepLog", "08:00");
String[] pieces = sleepString.split(":");
int sleepHour = Integer.parseInt(pieces[0]);
int sleepMinute = Integer.parseInt(pieces[1]);
String eveningString = sharedPref.getString("time_eveningLog", "20:00");
pieces = null;
pieces = eveningString.split(":");
int eveningHour = Integer.parseInt(pieces[0]);
Log.v(TAG, "eveningHour in registerAlarms: " + eveningHour);
int eveningMinute = Integer.parseInt(pieces[1]);
// create calendar objects pointing to the next time this clock will
// occur
Calendar sleepCal = Calendar.getInstance();
sleepCal.set(Calendar.HOUR_OF_DAY, sleepHour);
sleepCal.set(Calendar.MINUTE, sleepMinute);
sleepCal.set(Calendar.SECOND, 0);
Calendar eveningCal = Calendar.getInstance();
eveningCal.set(Calendar.HOUR_OF_DAY, eveningHour);
eveningCal.set(Calendar.MINUTE, eveningMinute);
sleepCal.set(Calendar.SECOND, 0);
Intent syncIntent = new Intent(this, SleepNotificationReceiver.class);
syncIntent.putExtra("MoodSleepLogAlarm", 0);
PendingIntent sleepPending = PendingIntent.getBroadcast(this, 0,
syncIntent, 0);
// then set the alarms
am.setRepeating(AlarmManager.RTC_WAKEUP, sleepCal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, sleepPending);
Log.v(TAG, "Alarm for sleep registered at " + sleepCal.getTime());
}
I log in the broadcastreceiver to check if it runs with:
Log.v(TAG, "Context: " + context.getClass().getName())
I can see this in logcat 4 seconds after starting my application as 0
6-27 17:59:29.492: V/SleepNotificationReceiver(2609): Context: android.app.ReceiverRestrictedContext
When I call the registerAlarms() via a button it doesn't happend. So it is only when called onCreate.
Why does it run the broadcast receiver after 4 seconds? (It does also run the broadcastreceiver with the same context given at the given times in my settings screen - but I need it to not run when I set it.)
When you are setting the alarm repeating, the second parameter indicates when the alarm has to go off the first time, so I think that if you type sleepCal.getTimeInMillis() and you are in the "future" compared to this time, the alarm is going off instantly.
Check that second parameter if it's in the future because I think that you are not setting the day or month in your calendar.
I am attempting to use CommonsWare's WakefulIntentService in a new application, specifically its ability to easily schedule the intent service to run at a later time.
I have a PreferenceActivity that allows the user to pick the schedule that the service is run (daily at 5am for example). Once the user makes a change to the preference value, I call:
AutoDownloadIntentServiceAlarmListener alarmListener = new AutoDownloadIntentServiceAlarmListener();
alarmListener.setForcedHour(5); // we want to schedule alarm for 5am everyday.
WakefulIntentService.scheduleAlarms(alarmListener, this, true);
For some reason, the desired IntentService (that extends WakefulIntentService) immediately starts up and performs its work.
Here is the implementation of of AutoDownloadIntentServiceAlarmListener:
public class AutoDownloadIntentServiceAlarmListener implements WakefulIntentService.AlarmListener {
private static final String TAG = "AutoDownloadIntentServiceAlarmListener";
private int mForcedHour = -1;
#Override
public long getMaxAge() {
return AlarmManager.INTERVAL_DAY * 2;
}
public void setForcedHour(int forcedHour) {
mForcedHour = forcedHour;
}
#Override
public void scheduleAlarms(AlarmManager alarmManager, PendingIntent pendingIntent, Context context) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
String autoDownloadTimePref = MyApplication.getInstance().getPrefs().getString("autoDownloadEpisodesSchedule", "0");
int hourOfAlarm = Integer.parseInt(autoDownloadTimePref);
// if this class has been created with a specific hour
// use it instead of the value obtained from SharedPreferences above.
if (mForcedHour > -1) {
Log.w(TAG, "Forced hour has been set for this AlarmListener. " + mForcedHour);
hourOfAlarm = mForcedHour;
}
calendar.set(Calendar.HOUR_OF_DAY, hourOfAlarm);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Log.d(TAG, String.format("Scheduled inexact alarm for %d", hourOfAlarm));
}
#Override
public void sendWakefulWork(Context context) {
Intent serviceIntent = new Intent(context, AutoDownloadIntentService.class);
WakefulIntentService.sendWakefulWork(context, serviceIntent);
}
}
It is my intention that the service does not startup as soon as it is scheduled, and instead starts up only at 5am the next day. (and continues to repeat on this schedule indefinitely, or until the user elects to disable or change its schedule)
What am I doing wrong?
It is my intention that the service does not startup as soon as it is scheduled, and instead starts up only at 5am the next day.
Except that's not what your code does, ~80% of the time. Your code says that it should run at 5am today, as you are getting the current time and not changing the day. Most of the time, 5am today is in the past, and so AlarmManager will immediately do its work.
You need to see if your calculated Calendar is older than now, and if so, add a day.