如何创建通知剩余为我的任务一旦我们在Edittext输入日期时间



大家早上好。我创建了Android应用程序来准备Schedule任务,我想为我的任务创建Notification Remainder,我没有任何代码来创建它…我谷歌这个话题,但我没有得到任何适当的解决方案,请帮助我任何人…提前感谢……

这里是我的代码…用于在edittext框中插入日期时间:

MainActivity.java:

 EditText edtdate;
  private ImageButton ib;
    private Calendar cal;
    private int day ,month ,year;
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_inbox);
edtdate = (EditText) findViewById(R.id.edtdate);
cal = Calendar.getInstance();
        day = cal.get(Calendar.DAY_OF_MONTH);
        month = cal.get(Calendar.MONTH);
        year = cal.get(Calendar.YEAR);
     cal.add(Calendar.HOUR, heure);                         
        Intent intent1 = new Intent(this, MyBroadcastReceiver.class);                       
        PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent1,   
      PendingIntent.FLAG_UPDATE_CURRENT);                       
        AlarmManager am = (AlarmManager) this .getSystemService(Context.ALARM_SERVICE);                         
        am.setRepeating(AlarmManager.RTC_WAKEUP, 
                cal.getTimeInMillis(), 
                heure*3600*1000,  sender);
      custom = new CustomDateTimePicker(this, new     
      CustomDateTimePicker.ICustomDateTimeListener() {
          @Override
          public void onSet(Dialog dialog, Calendar calendarSelected,
                  Date dateSelected, int year, String monthFullName,
                  String monthShortName, int monthNumber, int date,
                  String weekDayFullName, String weekDayShortName,
                  int hour24, int hour12, int min, int sec,
                  String AM_PM)
          {
              ((EditText) findViewById(R.id.edtdate))
                      .setText(calendarSelected
                                      .get(Calendar.DAY_OF_MONTH)
                              + "/" + (monthNumber+1) + "/" + year
                              + ", " + hour12 + ":" + min
                              + " " + AM_PM);
          }
          @Override
          public void onCancel() {
          }
      });
          }
      });
 custom.set24HourFormat(false);
        custom.setDate(Calendar.getInstance());
        findViewById(R.id.imageButton1).setOnClickListener(
                new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        custom.showDialog();
                    }
                });
}
  CustomDateTimePicker custom;

@odiiil您可以使用闹钟管理器在特定的日期时间安排通知

创建通知使用broadcastReceiver:

public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Intent resultIntent = new Intent(context,
            MainActivity.class);
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    // Because clicking the notification opens a new ("special") activity,
    // there's no need to create an artificial back stack.
    PendingIntent resultPendingIntent = PendingIntent.getActivity(context,
            0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    Notification.Builder mBuilder = new Notification.Builder(context)
    .setSmallIcon(R.drawable.courier_blanc)
    .setContentTitle("Title")
    .setContentText("Message");
    mBuilder.setAutoCancel(true);   
    mBuilder.setPriority(Notification.PRIORITY_MAX);
    mBuilder.setContentIntent(resultPendingIntent);
    // Sets an ID for the notification
    int mNotificationId = 001;
    // Gets an instance of the NotificationManager service
    NotificationManager mNotifyMgr = (NotificationManager) context
            .getSystemService(Application.NOTIFICATION_SERVICE);

    // Builds the notification and issues it.
    mNotifyMgr.notify(mNotificationId, mBuilder.build());
} }

说如果它有帮助。如果你有问题,或者不是你真正想要的,说出来吧:)

EDIT:

        int heure = 1;
        int minute = 30;
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR, heure); 
        cal.add(Calendar.MINUTE, minute);
        Intent intent = new Intent(this, MyBroadcastReceiver.class);
        PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager) this
                .getSystemService(Context.ALARM_SERVICE);
        am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), minute*60*1000+heure*3600*1000,  sender);

相关内容

最新更新