在我的应用程序中,我需要在重新启动后通过使用我的sharedprefs保存的Arraylist类中的日期重新启动所有由我设置的警报。但只有一个是由警报管理器设置的。我使用了BroadcastReciver和for循环。
ArrayList<DateList1> dateses;
Date dateFromLog;
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences sharedPreferences = context.getSharedPreferences("The Selected date", MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString("Date0list", null);
Type type = new TypeToken<ArrayList<DateList1>>() {
}.getType();
dateses = gson.fromJson(json, type);
if (dateses == null) {
dateses = new ArrayList<>();
}
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
Calendar calendar = Calendar.getInstance();
for (int i = 0; i<dateses.size(); i++){
calendar.setTime(dateses.get(i).getDate1());
Intent intent1 = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, i, intent1, PendingIntent.FLAG_ONE_SHOT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent);
intentArray.add(pendingIntent);
}
}
}
这是我将日期添加到Arraylist的方法
ArrayList<DateList1> Dates = new ArrayList<>();
@Override
protected void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
setContentView(R.layout.data_picker);
einfüGGen = (Button) findViewById(R.id.button3);
datums = (EditText) findViewById(R.id.datums);
fachs = (EditText) findViewById(R.id.fachs);
themens = (EditText) findViewById(R.id.themens);
GG = (TextView) findViewById((R.id.textView));
einfüGGen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent resultIntent = new Intent();
resultIntent.putExtra("datum", datums.getText().toString());
resultIntent.putExtra("fach", fachs.getText().toString());
resultIntent.putExtra("themen", themens.getText().toString());
setResult(RESULT_OK, resultIntent);
finish();
int y = Dates.size();
for (int i = 0; i<Dates.size(); i++){
Toast.makeText(EingabeFeld.this,"SOOOOSSS"+y,Toast.LENGTH_LONG).show();
}
}
});
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Toast.makeText(EingabeFeld.this, "Die Erinnerung wurde festgelegt für den "+dayOfMonth+"/"+month+"/"+year, Toast.LENGTH_SHORT).show();
Calendar c = Calendar.getInstance();
c.set(year,month,dayOfMonth);
c.set(Calendar.HOUR_OF_DAY,15);
c.set(Calendar.MINUTE,20);
c.set(Calendar.SECOND,0);
Date date = c.getTime();
Dates.add(1,new DateList1(date));
startAlarm(c);
saveData();
这是arraylist的实际类
public Date date1;
public DateList1(Date date){
date1 = date;
}
public Date getDate1() {
return date1;
}
}
我希望这能提前见到某人并致谢
答案 0 :(得分:0)
我已解决它:D。 我在MainActivity中完成了对ArrayList的添加和保存,并且通过startActivityForResult方法从SecondActivity获得了对MainActivity的支持。
感谢那些试图帮助我的人,我真的很喜欢你们,并且在使用无错误代码的情况下度过了愉快的时光; D