0

I am trying to set a Alarm. The user selects a time with the time picker and when time comes a Notification shows up. This works perfectly. But the problem is, the User Input the name of the alarm the user wrote doesn't show up. I do NOT get a error message!
https://prnt.sc/mfs5hx
https://prnt.sc/mfs5w2

NotificationHelper.java:

    public NotificationCompat.Builder getChannelNotification() {
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View frAl = inflater.inflate(R.layout.fragment_alarm, null);
    titelAlarm = frAl.findViewById(R.id.eingUserAlarm);
    String nameAlarm = titelAlarm.getText().toString();
    return new NotificationCompat.Builder(getApplicationContext(), channelID)
            .setContentTitle("Erinnerung")
            .setContentText(nameAlarm)
            .setSmallIcon(R.drawable.ic_one);
}

The EditText where the user can name the Alarm is inside a Fragment which can be accessed by a Navigation Drawer.

   <EditText
    android:id="@+id/eingUserAlarm"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="180dp"
    android:hint="Was wirst du machen?" />

Also the code for the Alarm is inside my Fragment.java.
Fragment.java:

Button buttonTimePicker = fraAla.findViewById(R.id.button_timepicker);
    buttonTimePicker.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DialogFragment timePicker = new AlarmTimePicker();
            timePicker.show(getChildFragmentManager(), "time picker");
        }
    });

    Button buttonCancelAlarm = fraAla.findViewById(R.id.button_cancel);
    buttonCancelAlarm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cancelAlarm();
        }
    });
    return fraAla;
}

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.HOUR_OF_DAY, hourOfDay);
    c.set(Calendar.MINUTE, minute);
    c.set(Calendar.SECOND, 0);

    updateTimeText(c);
    startAlarm(c);
}

private void updateTimeText(Calendar c) {
    String timeText = "Wir erinnern dich um ";
    timeText += DateFormat.getTimeInstance(DateFormat.SHORT).format(c.getTime());

    mTextView.setText(timeText);
}

private void startAlarm(Calendar c) {
    AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(getActivity(), AlertReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 1, intent, 0);

    if (c.before(Calendar.getInstance())) {
        c.add(Calendar.DATE, 1);
    }

    alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
}

private void cancelAlarm() {
    AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(getActivity(), AlertReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 1, intent, 0);

    alarmManager.cancel(pendingIntent);
    mTextView.setText("Wir werden nicht stören!");
}
pga009
  • 3
  • 4
  • You're inflating a new, separate instance of `fragment_alarm` right there. That instance is not the same one as was in the`Fragment` when you set the alarm, and it will only have the default values assigned in the XML on it. You will need to pass the necessary data to the alarm Receiver on the `Intent` you're creating for the `PendingIntent`, something like what is shown in [the question and answer here](https://stackoverflow.com/q/12470453). – Mike M. Feb 02 '19 at 20:26
  • Mike again you are really helping me thank you! – pga009 Feb 02 '19 at 20:30
  • @MikeM. so this didn't really work out for me still the same problem as I had before :/ – pga009 Feb 02 '19 at 20:48
  • tip: embed your pictures instead of linking to them. – PDiracDelta Feb 02 '19 at 22:13

0 Answers0