我如何使用datepicker在特定日期设置提醒



我有一个datepicker,可用于选择日期并将其设置为EditText。我如何在日期设置提醒?

在下面给出了从日期选择器设置递观的程序:

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

尝试以下:

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

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

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

相关内容

  • 没有找到相关文章

最新更新