我正在创建一个 java android 应用程序,我的应用程序每天在午夜运行一系列代码。
这实际上是我的问题的延续:前台服务是运行单个方法的唯一选择吗?(在@Muzammal_Ghafoor的回答下)
从上图中可以看出,创建PendingIntent时必须指定FLAG_MUTABLE或FLAG_IMMUTABLE。我很难理解 PendingIntent 的文档,所以我不知道如何解决这个问题。
这是我从给我的答案中添加的代码。对于上下文,PendingIntent 位于 ScheduleDailyTask() 函数上。
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();
}
}
}
您只需将
FLAG_MUTABLE
或 FLAG_IMMUTABLE
添加到您的 PendingIntent Flags 中,如下所示
PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE)
强烈建议您使用
FLAG_IMMUTABLE