如何在android中创建带警报的离线通知?

时间:2015-07-15 04:15:46

标签: android android-notifications

我创建了这个示例来完成我的工作,但是当我启动应用程序时会收到通知。我想在特定时间收到通知

public class TimeAlarm extends BroadcastReceiver {

        NotificationManager nm;
        long timeInMilliseconds;

        @SuppressWarnings("deprecation")
        @Override
        public void onReceive(Context context, Intent intent) {
              String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
              String givenDateString = date+" "+"09:38:00 GMT+05:30 2013"; 
              SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z yyyy");
              try {
                  Date mDate = sdf.parse(givenDateString);
                   timeInMilliseconds = mDate.getTime();
                  System.out.println("Date in milli :: " + timeInMilliseconds);
              } catch (ParseException e) {
                          e.printStackTrace();
              }
            nm = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            CharSequence from = "Brackfast";
            CharSequence message = "Its Time To Brackfast...";
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                    new Intent(), 0);
            Notification notif = new Notification(R.drawable.ic_launcher,
                    "Indian Food Diary...",timeInMilliseconds);

            notif.setLatestEventInfo(context, from, message, contentIntent);

            // Calendar cal = Calendar.getInstance();
            // SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            // String test = sdf.format(cal.getTime());
            // Log.e("TEST", test);
            // if(test.equalsIgnoreCase("9:18")){
            nm.notify(1, notif);

        }
    }

..........................     活动是:

public class CustomAlarm extends Activity {

         AlarmManager am;

         long timeInMilliseconds;
         @Override
         public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.custom_alarm);
          am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
          setOneTimeAlarm();
          String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
          String givenDateString = date+" "+"09:34:00 GMT+05:30 2013"; 
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z yyyy");
          try {
              Date mDate = sdf.parse(givenDateString);
               timeInMilliseconds = mDate.getTime();
              System.out.println("Date in milli :: " + timeInMilliseconds);
          } catch (ParseException e) {
                      e.printStackTrace();
          }
         }

         public void setOneTimeAlarm() {
          Intent intent = new Intent(this, TimeAlarm.class);
          PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
            intent, PendingIntent.FLAG_ONE_SHOT);
          am.set(AlarmManager.RTC_WAKEUP,
                  timeInMilliseconds, pendingIntent);
         }

         public void setRepeatingAlarm() {
          Intent intent = new Intent(this, TimeAlarm.class);
          PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
            intent, PendingIntent.FLAG_CANCEL_CURRENT);
          am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
            (5 * 1000), pendingIntent);
         }

        }

0 个答案:

没有答案