java - 如何为具有特定日期的用户配置通知,然后将其存储在房间数据库中

标签 java android android-notifications android-room android-switch

我正在开发约会通知,我想将其存储在我的数据库中。我有一个“通知”类,并且有约会界面。 所以,我需要的是,当检查开关时,通知将处于 Activity 状态,并且当这一天到来时它会显示。 这是我的代码:

约会.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>

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

通知.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;
}
  }

最佳答案

我附近没有笔记本电脑,所以这里是这一切如何发生的要点:

  1. 当用户输入约会时,将其添加到 Room db 中。使用 following创建数据库的教程。
  2. 运行后台工作管理器作业计划,这将:
    • 全天每 x 分钟从房间数据库检索日期时间信息。
    • 如果日期/时间等于当前日期时间,则会显示通知。

结帐thisthis gist from my app了解如何创建这样的工作管理器。

关于java - 如何为具有特定日期的用户配置通知,然后将其存储在房间数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56751842/

相关文章:

java - 从java传递命令提示符函数

android - 运行 google script api quickstart for android 时出错

java - 如何让Android使用spring监听来自服务器的请求

Android Activity 不断恢复保存的实例状态

android - 如何监听我的应用程序和日历应用程序何时收到通知?

java - MySQL/Hibernate - 如何调试不断掉线的 MySQL 池连接?

java - 创建guava TreeMultimap的子图并对其键值对进行高效迭代

java - HTML 基本元素不会将上下文附加到来自 JSP 标记文件的头链接

android - 从用户的声音中获取振幅

android - 杀死 Activity 的所有实例