I have set up an Alarm Manager that is supposed to trigger a Pending Intent at a certain time of day.
I placed the code in the onCreate() method of my Main Activity, as I believed this was the best place to put it?
Below is the code:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 4); // trigger at 4am in the morning
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
final Intent updateIntent = new Intent(Intent.ACTION_MAIN, null);
updateIntent.addCategory(Intent.CATEGORY_HOME);
final ComponentName cn = new ComponentName(
"com.example.myotherapp",
"com.example.myotherapp.MainActivity");
updateIntent.setComponent(cn);
updateIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, updateIntent, 0);
AlarmManager alarm = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pIntent); // launch at 4am, then every day (24 hours)
The Alarm Manager works, I can see it fire at the specified time, but it also seems that the Pending Intent is fired as soon as this base app starts.
What is causing the Intent to fire right away? And how can I stop it from doing so?