【问题标题】:How To Create Notification Remainder For my Task once after we entered DateTime in Edittext在 Edittext 中输入 DateTime 后,如何为我的任务创建通知剩余
【发布时间】:2014-07-24 20:48:13
【问题描述】:

大家早上好..我创建了 Android 应用程序来准备计划任务..对于我的任务我想为我的任务创建通知剩余部分..我没有任何编码来创建它...我在谷歌上搜索这个主题但是我没有得到任何适当的解决方案..请任何人帮助我...提前谢谢..

这是我的编码..用于在编辑文本框中插入日期时间:

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;

【问题讨论】:

    标签: java android notifications


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      使用广播接收器创建通知:

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

      说是否有帮助。如果您有一些问题或者不是您真正想要的,请说出来:)

      编辑:

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

      【讨论】:

      • 感谢您的回复..通知类没问题...在我的应用程序中我正在使用 edittext 为我的任务添加日期时间..一旦我单击添加按钮..希望每天发送剩余通知到用户邮件ID,直到任务完成..希望你理解我的问题..
      • 所以使用警报管理器来完成他的工作:Calendar cal = Calendar.getInstance();cal.add(Calendar.HOUR, heure); 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(), heure*3600*1000, sender);
      • 对不起,我没有把代码写对,但是复制粘贴它并说它是否是你想要的(不确定理解......)
      • 您必须停止通知:Intent notificationIntent = new Intent(this, ActivityStopReciever.class); notificationIntent.setFlags(Notification.FLAG_AUTO_CANCEL); PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0); 并且在您的活动中停止接收器,您必须调用取消 [链接]developer.android.com/reference/android/app/… 并在此处查找停止警报:stackoverflow.com/questions/4963271/…
      • 我已经把我所有的代码都放在这里了......(我编辑了我的答案)你把它放在你的清单中了吗? ` `
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      • 2022-12-08
      • 2016-06-24
      • 1970-01-01
      相关资源
      最近更新 更多