I am creating a java android application where my app runs a series of codes at midnight, everyday.
This is actually a continuation of my question here: Is Foreground Service the only option for running a single method? (under @Muzammal_Ghafoor's answer)
I have recreated the code that he gave me but now I came across a problem.
From the image above, it is stated that FLAG_MUTABLE or FLAG_IMMUTABLE must be specified when creating a PendingIntent. I am having a hard time understanding the documentation of PendingIntent so I have no idea how to fix this.
This is the code that I added from the answer that was given to me. For context, the PendingIntent is located on the scheduleDailyTask() function.
MainActivity.java // onCreate()
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
scheduleDailyTask(this);
}
MainActivity.java // scheduleDailyTask()
public void scheduleDailyTask(Context context){
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, DailyTaskBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_NO_CREATE | PendingIntent.FLAG_IMMUTABLE);
// Alarm Schedule
if (pendingIntent == null) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.add(Calendar.DAY_OF_YEAR, 1); // 1 = next day
// Set the alarm to trigger at midnight every day
alarmManager.setRepeating(
AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY,
PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
);
}
}
DailyTaskBroadcastReceiver.java
public class DailyTaskBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
storage.createNewDailyCSV(context);
}
}
storage.java // createNewDailyCSV()
public static void createNewDailyCSV(Context context){
try {
// This creates the directories needed for the file
ContextWrapper contextWrapper = new ContextWrapper(context);
Date dayToday = Calendar.getInstance().getTime();
String year = DayData.formatYEAR(dayToday);
String month = DayData.formatMONTH(dayToday);
dailyFileName = DayData.formatDAY(dayToday);
dailyFilePath = new File(contextWrapper.getExternalFilesDir(year + "/" + month), dailyFileName);
// This creates the CSV and adds all the needed information inside it
loadConstCSV();
PrintWriter outputFile = new PrintWriter(dailyFilePath);
StringBuilder sb = new StringBuilder();
String name;
String price;
String weekday = DayData.formatWEEKDAY(dayToday);
boolean[] weekdayBOOL;
for (int i=0; i<Data.constantList.size();i++){
name = Data.constantList.get(i).getName();
price = String.valueOf(Data.constantList.get(i).getPrice());
weekdayBOOL = Data.constantList.get(i).getWd_Bool();
switch (weekday){
case "Sun":
if (weekdayBOOL[0]){
sb.append(name).append(",").append(price).append("\n");
}
break;
case "Mon":
if (weekdayBOOL[1]){
sb.append(name).append(",").append(price).append("\n");
}
break;
case "Tue":
if (weekdayBOOL[2]){
sb.append(name).append(",").append(price).append("\n");
}
break;
case "Wed":
if (weekdayBOOL[3]){
sb.append(name).append(",").append(price).append("\n");
}
break;
case "Thu":
if (weekdayBOOL[4]){
sb.append(name).append(",").append(price).append("\n");
}
break;
case "Fri":
if (weekdayBOOL[5]){
sb.append(name).append(",").append(price).append("\n");
}
break;
case "Sat":
if (weekdayBOOL[6]){
sb.append(name).append(",").append(price).append("\n");
}
break;
}
outputFile.write(sb.toString());
outputFile.close();
}
}catch (Exception e){
e.printStackTrace();
}finally {
Data.constantList.clear();
}
}
}
Please update your code in the scheduleDailyTask() and try to compile the project.
alarmManager.setRepeating(
AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY,
PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT)
);
Note: If your project targeting 31 or above android version you must specify either the FLAG_IMMUTABLE
or FLAG_MUTABLE
.