首页 > 解决方案 > 从 ArrayList 设置具有多个日期的多个警报

问题描述

在我的应用程序中,我需要在重新启动之前设置的所有警报后启动,方法是使用我通过 sharedprefs 保存的 Arraylist 类中的日期。但只有一个是由 Alarmmanager 设置的。我使用了 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;

}

}

我希望这会见到某人并提前致谢

标签: javaandroiddatealarmmanager

解决方案


我已经解决了:D。我在我的 MainActivity 中添加了 ArrayList 和保存部分,并通过 startActivityForResult 方法从我的 SecondActivity 中获取了我的 MainActivity 的 long。

感谢那些试图帮助我的人,我真的很喜欢你们,并且用无错误的代码度过了愉快的一天;D


推荐阅读