Я разрабатываю уведомление о встрече и хочу сохранить его в своей базе данных. У меня есть класс "Уведомление", и у меня есть интерфейс назначения.
Итак, что мне нужно, это когда переключатель проверен, уведомление будет активным, а когда наступит день, оно будет отображаться.
Это мой код:
Appointment.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AddEditRdv">
<CalendarView
android:id="@+id/cv"
android:layout_width="288dp"
android:layout_height="327dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:minDate="06/12/2019"
android:selectedWeekBackgroundColor="@color/Bleu"
android:unfocusedMonthDateColor="@color/a"
android:weekNumberColor="@color/a"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_ajouter"
android:layout_width="90dp"
android:layout_height="74dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="33dp"
android:background="@drawable/circle"
android:text="Valider"
android:textColor="@color/Bleu"
android:textSize="18sp"
android:textStyle="bold"
android:typeface="serif"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.99" />
<EditText
android:id="@+id/titre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:ems="10"
android:hint="titre"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.393"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/heure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="105dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="103dp"
android:editable="false"
android:ems="10"
android:enabled="true"
android:freezesText="true"
android:hint="Entrer l'heure"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/cv"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.393"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.287" />
<Switch
android:id="@+id/switch1"
android:layout_width="139dp"
android:layout_height="21dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="17dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="8dp"
android:text="Notification"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.802" />
</android.support.constraint.ConstraintLayout>
Appointment.java
public class AddEditRdv extends AppCompatActivity {
Button btn_ajouter ;
CalendarView cv ;
TextView showTime;
TimePickerDialog timePicker;
EditText heure,titre;
int année , mois , jour , hour , minute;
String date , time ;
public static final String Extra_Titre ="com.example.mjg.Extra_Titre";
public static final String Extra_DateR="com.example.mjg.Extra_DateR";
public static final String Extra_HeureR ="com.example.mjg.Extra_HeureR";
private Switch aSwitch;
Calendar c;
SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rdv);
cv= (CalendarView) findViewById(R.id.cv);
btn_ajouter = (Button) findViewById(R.id.btn_ajouter);
heure = (EditText) findViewById(R.id.heure);
titre = (EditText) findViewById(R.id.titre);
c= Calendar.getInstance();
choisirHeure();
choisirDate();
btn_ajouter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent data = new Intent();
data.putExtra(Extra_Titre,titre.getText().toString());
data.putExtra(Extra_DateR,date);
data.putExtra(Extra_HeureR,time);
setResult(RESULT_OK,data);
finish();
}
});
aSwitch = findViewById(R.id.switch1);
aSwitch.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if(isChecked == true){
Toast.makeText(getApplicationContext(), "Checked !",
Toast.LENGTH_LONG).show();
//Activate Alarm
AlarmManager alarmManager = (AlarmManager)
getSystemService(Context.ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP,c.DATE
,pendingIntent);
}else {
Toast.makeText(getApplicationContext(), "Unchecked !",
Toast.LENGTH_LONG).show();
}
}
});
}
public void choisirDate()
{
cv.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(@NonNull CalendarView view, int
year, int month, int dayOfMonth) {
jour = dayOfMonth;
mois = month ;
année = year;
c.set(Calendar.DAY_OF_MONTH,dayOfMonth);
c.set(Calendar.MONTH,month);
c.set(Calendar.YEAR,year);
date = jour + "/" + (mois + 1) + "/" + année ;
System.out.println(date);
}
});
}
public void choisirHeure()
{
heure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timePicker = new TimePickerDialog(AddEditRdv.this, new
TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int
selectedHour, int selectedMinute) {
hour = selectedHour ;
minute = selectedMinute ;
time = hour + ":" + minute;
System.out.println(time);
heure.setHint(time);
}
}, hour, minute, true);
timePicker.setTitle("Choose time");
timePicker.show();
}
});
}
}
Notification.java
@Entity
public class Notification {
@PrimaryKey(autoGenerate = true)
private int idN;
private String date,heure,information;
public Notification(String date, String heure, String information) {
this.date = date;
this.heure = heure;
this.information = information;
}
}