How can I use a datepicker to set a reminder on a specific date?

时间:2017-05-03 14:29:31

标签: java android datepicker reminders

I have a datepicker which can be used to pick a date and set it to a edittext. how can i set a reminder on the date ?.

The program for setting a edittext from the date picker is given below:

import android.app.DatePickerDialog;
import android.content.DialogInterface; 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

   Calendar myCalendar= Calendar.getInstance();
   private EditText et;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      et =(EditText)findViewById(R.id.editText3);
    }

  DatePickerDialog.OnDateSetListener date =new 
  DatePickerDialog.OnDateSetListener(){
     @Override
     public void onDateSet(DatePicker view, int year, int monthOfYear,
                           int dayOfMonth) {
         // TODO Auto-generated method stub
         myCalendar.set(Calendar.YEAR, year);
         myCalendar.set(Calendar.MONTH, monthOfYear);
         myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
         updateLabel();
     }
 };

 public void onClick(View v) {
     // TODO Auto-generated method stub
     new DatePickerDialog(MainActivity.this, date, myCalendar
             .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
             myCalendar.get(Calendar.DAY_OF_MONTH)).show();

 }



 private void updateLabel() {

     String myFormat = "MM/dd/yy"; //In which you need put here
     SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

     et.setText(sdf.format(myCalendar.getTime()));
  }
}

1 个答案:

答案 0 :(得分:1)

试试这个:

private void setAlarm(Calendar target){
    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 1, intent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, target.getTimeInMillis(), pendingIntent);
}

让我知道它是否对你有帮助。

<强>更新 alarmReciever必须是这样的:

 public class AlarmReceiver extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {

  Toast.makeText(context, 
   "AlarmReceiver.onReceive()", 
   Toast.LENGTH_LONG).show();
 }

}

并将其添加到manifest.xml标记内的application

<receiver android:name=".AlarmReceiver" android:process=":remote" />