如何从日期选择器触发AlarmManger?

时间:2017-01-23 01:31:40

标签: android datepicker alarmmanager android-notifications

我正在制作一个简单的TO-DO-List,而我正试图给我的AlarmManager提供它将触发的日期。所以我从datePicker传递数据。最后,我想使用我的Receiver类显示通知。 注意:我是一个Android初学者,这是我的第二个应用程序。

这是我的datePicker(OnDateListener方法)

    private DatePickerDialog.OnDateSetListener dPickerListner=new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        yearx=year;
        monthx=month +1;
        dayx=dayOfMonth;
        //save in the database
        database=new Database(getBaseContext());
        String subj=WrittenSubj.getText().toString();
        String deta=WrittenDeta.getText().toString();
        String date=dayx+"/"+monthx;
        theTask.setDate(date);
        theTask.setSubject(subj);
        theTask.setDetails(deta);
        Intent alertIntent=new Intent(getBaseContext(),TaskAlert.class);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, monthx);
        calendar.set(Calendar.DAY_OF_MONTH, dayx);
        calendar.set(Calendar.HOUR_OF_DAY, 3);
        calendar.set(Calendar.MINUTE, 14);
        calendar.set(Calendar.SECOND, 55);


        AlarmManager alarmManager=(AlarmManager)
                getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),
                PendingIntent.getBroadcast(getBaseContext(),1,alertIntent,PendingIntent.FLAG_UPDATE_CURRENT));

        database.Insert(theTask);
        Intent BackToMain=new Intent(getBaseContext(),MainActivity.class);
        startActivity(BackToMain);
        finish();
        Toast.makeText(TaskDetails.this, "Date saved",Toast.LENGTH_LONG).show();


    }


};

这是我的接收者课程

    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.support.v4.app.NotificationCompat;
    import java.util.ArrayList;

  public class TaskAlert extends BroadcastReceiver {
      ArrayList<TheTask> theTasks=new ArrayList<>();
       @Override
      public void onReceive(Context context, Intent intent) {
       CreateNotification(context,"Your Task","You have a task","Task");


}
public void CreateNotification(Context context, String msg, String msgText, String msgAlert){
    PendingIntent notifictIntent = PendingIntent.getActivity(context,0,new Intent(context,MainActivity.class),0);
    NotificationCompat.Builder builder=new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_stat_name)
            .setContentTitle(msg)
            .setTicker(msgText)
            .setContentText(msgAlert);
    builder.setContentIntent(notifictIntent);
    builder.setDefaults(NotificationCompat.DEFAULT_SOUND);
    builder.setAutoCancel(true);
    NotificationManager notificationManager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1,builder.build());

}}

0 个答案:

没有答案