任何人都可以帮我解决这个问题。以及我需要做的是设置警报通知的日期和时间,并将其存储在sqlite数据库中以显示在recyclerview中。
现在我的问题是我不知道如何设置闹钟。我有我的日期选择器和时间戳工作但我不知道如何设置闹钟,所以任何人都可以告诉我如何设置它?
继承我的代码
public class set_events extends AppCompatActivity {
Register_pet_database myDB;
Spinner event_pet;
Button btn_setevent;
eventsAdapter adapter;
EditText datepicker, time, note;
DatePickerDialog.OnDateSetListener date;
Toolbar toolbar;
Calendar myCalendar=Calendar.getInstance();
TextView tf1, tf2, tf3,tf4;
ArrayList<events_counter> arrayList= new ArrayList<>();
events_counter alert;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_events);
myDB=new Register_pet_database(this);
btn_setevent = (Button) findViewById(R.id.btn_seteventnow);
datepicker = (EditText) findViewById(R.id.takedate);
time = (EditText) findViewById(R.id.taketime);
note = (EditText) findViewById(R.id.mynote);
event_pet=(Spinner)findViewById(R.id.eventlist);
toolbar=(Toolbar)findViewById(R.id.set_toolbar_events);
tf1=(TextView)findViewById(R.id.setevents);
tf2=(TextView)findViewById(R.id.setdates);
tf3=(TextView)findViewById(R.id.settimes);
tf4=(TextView)findViewById(R.id.setnotes);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
AddEvent();
Typeface font = Typeface.createFromAsset(this.getAssets(), "fonts/agency-fb.ttf");
tf1.setTypeface(font);
Typeface font1 = Typeface.createFromAsset(this.getAssets(), "fonts/agency-fb.ttf");
tf2.setTypeface(font1);
Typeface font2 = Typeface.createFromAsset(this.getAssets(), "fonts/agency-fb.ttf");
tf3.setTypeface(font2);
Typeface font3 = Typeface.createFromAsset(this.getAssets(), "fonts/agency-fb.ttf");
tf4.setTypeface(font3);
final DatePickerDialog.OnDateSetListener date= new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, month);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
datepicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DatePickerDialog(set_events.this, date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar currentTime= Calendar.getInstance();
final int hour = currentTime.get(Calendar.HOUR_OF_DAY);
final int minute = currentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker= new TimePickerDialog(set_events.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
if(hourOfDay<12 && hourOfDay >=0){
time.setText( hourOfDay + ":" + minute+" AM");
}
else{
hourOfDay-=12;
if(hourOfDay==0){
hourOfDay=12;
}
time.setText( hourOfDay + ":" + minute+" PM");
}
}
},hour,minute,true);
mTimePicker.show();
}
});
final ArrayAdapter<CharSequence> adapter;
adapter = ArrayAdapter.createFromResource(this, R.array.events, R.layout.support_simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
event_pet.setAdapter(adapter);
event_pet.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(position==0){
note.setText("Im going to feed my pet now");
}
if(position==1){
note.setText("Im going to take my pet to the veterinarian!");
}
if(position==2){
note.setText("Im going to take my pet for a walk !");
}
if(position==3){
note.setText("Im going to take my pet to the groomers !");
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void updateLabel() {
String myFormat = "MM/dd/yy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat);
datepicker.setText(sdf.format(myCalendar.getTime()));
}
private void AddEvent(){
btn_setevent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
arrayList=new ArrayList<>();
adapter=new eventsAdapter(set_events.this,arrayList);
alert= new events_counter();
alert.setEvent_name(event_pet.getSelectedItem().toString());
alert.setEvent_date(datepicker.getText().toString());
alert.setEvent_time(time.getText().toString());
alert.setEvent_note(note.getText().toString());
Boolean insertEvent=myDB.insertDataToEvent(alert);
try{
if (insertEvent = true) {
adapter.add(alert);
Toast.makeText(set_events.this, "Your event has been Registered", Toast.LENGTH_LONG).show();
Intent intent = new Intent(set_events.this, eventlist.class);
startActivity(intent);
finish();
} else {
Toast.makeText(set_events.this, "Your event has not been Registerd", Toast.LENGTH_LONG).show();
}
}catch (Exception e){
e.printStackTrace();
}
}
});
}
@Override
public void onBackPressed() {
Intent intent = new Intent(set_events.this, eventlist.class);
intent.putExtra("Tab", 1);
startActivity(intent);
finish();
}
}
答案 0 :(得分:0)
您可以在Calendar
变量中设置时间和日期,如下所示:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0 );
calendar.set(Calendar.MINUTE, 1);
然后像这样设置闹钟:
alarmmgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar, INTERVAL, pendingintent);