在android studio中使用alarm manager设置特定日期和时间的通知

ewm0tg9j  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(466)

我的应用程序上有datepicker、time picker和edittext。我想用edittext变量设置特定日期和时间的通知。它可以工作,但它只能工作时间选择器。它使用edittext-only计时器变量设置通知。如何使用时间和日期选择器变量设置通知。
主活动类

public class mainactivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener, DatePickerDialog.OnDateSetListener {
    Calendar now = Calendar.getInstance();
    Button hatirlaticitarihsaat,backhome;
    EditText editisim,editaciklama;
    TimePickerDialog tpd;
    DatePickerDialog dpd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hatirlatici);

        editisim=findViewById(R.id.hatbaslik);     
        editaciklama=findViewById(R.id.hataciklama);
        hatirlaticitarihsaat= findViewById(R.id.taarihsaatbutton);
        hatirlaticitarihsaat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogFragment datePicker = new DatePickerFragment();
                datePicker.show(getSupportFragmentManager(), "date picker");
            }
        });
    }

    public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {

        Intent intent = new Intent(hatirlatici.this, AlertReciever.class);
        intent.putExtra("notificationId", notificationId);
        intent.putExtra("messagex", editaciklama.getText().toString());
        intent.putExtra("titlex", editisim.getText().toString());
        intent.setAction("ALERT_RECIEVER");

        PendingIntent alarmintent = PendingIntent.getBroadcast(
                hatirlatici.this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT
        );
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        Calendar startalarm = Calendar.getInstance();
        startalarm.set(Calendar.HOUR_OF_DAY, hourOfDay);
        startalarm.set(Calendar.SECOND, 0);
        startalarm.set(Calendar.MINUTE, minute);
        long alarmstartTime = startalarm.getTimeInMillis();
        alarmManager.set(AlarmManager.RTC_WAKEUP, alarmstartTime, alarmintent);
 }
    private void startNotification(Calendar now){
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this,AlertReciever.class);
        intent.putExtra("Isım_ID", editisim.getText().toString());
        intent.putExtra("Aciklama", editaciklama.getText().toString());
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this,1,intent,0);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), pendingIntent);
    }

    @Override
    public void onDateSet(DatePicker datePicker, int year, int month, int day) {
        now.set(Calendar.YEAR, year);
        now.set(Calendar.MONTH, month);
        now.set(Calendar.DAY_OF_MONTH, day);
        DialogFragment timePicker = new TimePickerFragment();
        timePicker.show(getSupportFragmentManager(), "time picker");
        String Datex = DateFormat.getDateInstance(DateFormat.DATE_FIELD).format(now.getTime());
        zaman.setText(Datex);
        Calendar startalarm = Calendar.getInstance();
        startalarm.set(Calendar.DAY_OF_MONTH,day );
        startalarm.set(Calendar.YEAR, year);
        startalarm.set(Calendar.MONTH, month);
    }
}

timepickerfragment.java文件

public class TimePickerFragment extends DialogFragment {

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour,minute, android.text.format.DateFormat.is24HourFormat(getActivity()));
    }
}

datepickerfragment.java文件

public class DatePickerFragment extends DialogFragment {
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener) getActivity(),year ,month,day);
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题