alarm manager使用edittext变量以特定日期和时间设置通知

cvxl0en2  于 2021-07-07  发布在  Java
关注(0)|答案(0)|浏览(240)

我的项目有日期选择器,时间选择器,编辑文本。我可以选择日期和时间,我可以设置通知,但ı只在选定的时间工作,例如ı 设置大量的通知只能对某些部分起作用,并且它们对错误的编辑文本变量起作用。你怎么能ı 在选定的时间和日期上使用我的edittext变量设置多个通知。这很重要。我将在5天内解决我的问题。你能快点吗。拜托
主活动.java

public class MainActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener, DatePickerDialog.OnDateSetListener {
    Calendar now = Calendar.getInstance();
    Button hatirlaticitarihsaat,backhome,restore;
    EditText editisim,editaciklama;
    ImageView tickx;
    TextView textbasariylakuruldu,texthatirlaticibas,hatirlaticiaciktext,tarih;
    TimePickerDialog tpd;
    DatePickerDialog dpd;
    private int notificationId = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hatirlatici);
        tarih=findViewById(R.id.tarihzaman);
 tickx=findViewById(R.id.tick);
        editisim=findViewById(R.id.hatbaslik);
        textbasariylakuruldu=findViewById(R.id.textbasariylakuruldu);
        texthatirlaticibas=findViewById(R.id.texthatirlaticibas);
        hatirlaticiaciktext=findViewById(R.id.hatirlaticiaciktext);
        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) {

        now.set(Calendar.HOUR_OF_DAY, hourOfDay);
        now.set(Calendar.MINUTE, minute);
        now.set(Calendar.SECOND, 0);
        String isim = editisim.getText().toString();
String aciklamax = editaciklama.getText().toString();
Calendar startalarm = Calendar.getInstance();
        startalarm.set(Calendar.HOUR_OF_DAY, hourOfDay);
        startalarm.set(Calendar.SECOND, 0);
        startalarm.set(Calendar.MINUTE, minute);
        startNotification(now);

    }

    private void startNotification(Calendar now){
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this,MainActivity.class);
        intent.setAction("ALERT_RECIEVER");
        intent.putExtra("notificationId", notificationId);
        intent.putExtra("titlex", editisim.getText().toString());
        intent.putExtra("messagex", 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) {

        DialogFragment timePicker = new TimePickerFragment();
        timePicker.show(getSupportFragmentManager(), "time picker");
now.set(Calendar.DAY_OF_MONTH,day );
        now.set(Calendar.YEAR, year);
        now.set(Calendar.MONTH, month);
 Intent intent = new Intent(hatirlatici.this, AlertReciever.class);
        PendingIntent alarmintent = PendingIntent.getBroadcast(
                hatirlatici.this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT
        );

    }

}

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);

    }
}

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()));
    }
}

AlertReceiver.java文件

public class AlertReciever extends BroadcastReceiver {
    private Notification notification;
    private static final String ChannelID = "Sample_Channel";

    @Override
    public void onReceive(Context context, Intent intent) {
        int NotificationId = intent.getIntExtra("notificationId", 0);
        String message = intent.getStringExtra("messagex");
        String title = intent.getStringExtra("titlex");
        Intent mainIntent = new Intent(context, hatirlatici.class);
        PendingIntent contentIntent = PendingIntent.getActivity(
                context,0,mainIntent,0
        );
        NotificationManager notificationManager =
                (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
            CharSequence channelName = "My Notification";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(ChannelID,channelName,importance);
            notificationManager.createNotificationChannel(channel);

        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, ChannelID)
                .setContentText(message)
                .setContentTitle(title)
                .setContentIntent(contentIntent)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setSmallIcon(R.drawable.ic_baseline_menu_book_24);

        notificationManager.notify(NotificationId, builder.build());
    }

}

暂无答案!

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

相关问题