多个通知和意图问题

时间:2012-11-24 05:56:26

标签: android android-intent notifications alarmmanager statusbar

我正在开发一个用户可以安排培训日期和时间的应用程序。用户可以在使用应用程序时安排多个培训日期和时间。比如说,用户将安排两次训练,TR1和TR2。当TR1日期和时间匹配时,将向用户发送通知并点击通知,用户将被引导至培训活动。 TR2在达到培训的日期和时间时也会发生同样的情况。

为了安排通知,我使用 AlarmManager 。我面临的问题是,如果我安排多次培训,我只会收到最后一次通知。

生成通知的活动

public class SettingsActivity extends Activity {
    //objects for use
    DateFormat fmtDateAndTime=DateFormat.getDateTimeInstance();
      TextView dateAndTimeLabel;
      Calendar dateAndTime=Calendar.getInstance();
      ImageView confirm;
      String dateTime;

      EditText duration;

      //Alarm manager object for notification
      AlarmManager am;


      //notification date and time
      int hour;
      int minute;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings_portrait);
        duration=(EditText)findViewById(R.id.duration_editText);
        dateAndTimeLabel=(TextView)findViewById(R.id.timeTxt);

        updateLabel();
        updateNotification();
        am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

     // On Click Confirm

            confirm=(ImageView)findViewById(R.id.confirm_imageView);
            confirm.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    getDateTime();

                   setOneTimeAlarm();
                }
            });

      }
      //date picker
      public void chooseDate(View v) {
        new DatePickerDialog(SettingsActivity.this, d,
                              dateAndTime.get(Calendar.YEAR),
                              dateAndTime.get(Calendar.MONTH),
                              dateAndTime.get(Calendar.DAY_OF_MONTH))
          .show();
      }
      //time picker
      public void chooseTime(View v) {
        new TimePickerDialog(SettingsActivity.this, t,
                              dateAndTime.get(Calendar.HOUR_OF_DAY),
                              dateAndTime.get(Calendar.MINUTE),
                              true)
          .show();
      }

      private void updateLabel() {
        dateAndTimeLabel.setText(fmtDateAndTime
                                  .format(dateAndTime.getTime()));
      }

      DatePickerDialog.OnDateSetListener d=new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
          dateAndTime.set(Calendar.YEAR, year);
          dateAndTime.set(Calendar.MONTH, monthOfYear);
          dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);
          updateLabel();
          updateNotification();
        }
      };

      TimePickerDialog.OnTimeSetListener t=new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay,
                              int minute) {
          dateAndTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
          dateAndTime.set(Calendar.MINUTE, minute);
          updateLabel();
          updateNotification();

        }
      };  




   // Notification Function
//      private void Notification(String notificationTitle, String notificationMessage)
//      {
//          NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//          Notification notification = new Notification(R.drawable.ic_launcher, "Training Scheduled!", System.currentTimeMillis());
//          //on click notification goto dinnerden activity
//          Intent notificationIntent = new Intent(this, SettingsActivity.class);
//          PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
//   
//          notification.setLatestEventInfo(SettingsActivity.this, notificationTitle, notificationMessage, pendingIntent);
//          notificationManager.notify(10001, notification);
//      }
      //getting date time from textview
      void getDateTime()
      {
         dateTime= dateAndTimeLabel.getText().toString()+" Duration HRS: "+duration.getText().toString();

         makeAToast(dateTime);
         //Notification("Training Scheduled at:",dateTime);

      }

      void updateNotification()
      {
          hour= dateAndTime.get(Calendar.HOUR_OF_DAY);
          minute=dateAndTime.get(Calendar.MINUTE);
      }



      public void setOneTimeAlarm() {

        // the time set
         Calendar calendar = Calendar.getInstance();
//          hour= dateAndTime.get(Calendar.HOUR_OF_DAY);
//          minute=dateAndTime.get(Calendar.MINUTE);
        // int hour= dateAndTime.get(Calendar.HOUR_OF_DAY);
        // int minute=dateAndTime.get(Calendar.MINUTE);
         // noon tomorrow
         //calendar.add(Calendar.DAY_OF_YEAR, 1);
         calendar.set(Calendar.HOUR_OF_DAY, hour);
         calendar.set(Calendar.MINUTE, minute);
         calendar.set(Calendar.SECOND, 0);





      Intent intent = new Intent(this, TimeAlarm.class);
      PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
        intent, PendingIntent.FLAG_ONE_SHOT);
      am.set(AlarmManager.RTC_WAKEUP,
       calendar.getTimeInMillis(), 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);
     }








      public void makeAToast(String str) {

        Toast toast = Toast.makeText(this,str, Toast.LENGTH_LONG);
        toast.setGravity(Gravity.BOTTOM, 0, 0);
        toast.setDuration(1000000);
        toast.show();
    }
}

创建通知

public class TimeAlarm extends BroadcastReceiver {

     NotificationManager nm;

     @Override
     public void onReceive(Context context, Intent intent) {
      nm = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);
      CharSequence from = "Scheduled Training";
      CharSequence message = "Please attend the scheduled training";
     // Intent welcomeIntent = new Intent(context, CustomAlarm.class);
      PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
        new Intent(), 0);
      Notification notif = new Notification(R.drawable.ic_launcher,
        "Crazy About Android...", System.currentTimeMillis());
      notif.setLatestEventInfo(context, from, message, contentIntent);
      nm.notify(1, notif);
     }
    }

活动的布局

<?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="488dp" >

            <ImageView
                android:id="@+id/confirm_imageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:src="@drawable/schedule_button" />

            <TextView
                android:id="@+id/timeTxt"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginTop="41dp"
                android:text="@string/time_show" />

            <Button
                android:id="@+id/timeBtn"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/timeTxt"
                android:layout_marginTop="34dp"
                android:onClick="chooseTime"
                android:text="@string/time" />

            <Button
                android:id="@+id/dateBtn"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/timeBtn"
                android:layout_marginTop="38dp"
                android:onClick="chooseDate"
                android:text="@string/date" />

            <EditText
                android:id="@+id/duration_editText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/dateBtn"
                android:layout_marginTop="36dp"
                android:ems="10"
                android:hint="@string/duration_hint"
                android:inputType="number" />

            <TextView
                android:id="@+id/duration_textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/duration_editText"
                android:layout_alignBottom="@+id/duration_editText"
                android:layout_alignParentLeft="true"
                android:text="@string/duration"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </RelativeLayout>

    </LinearLayout>
    </ScrollView>

manifest.xml 的一部分:

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name="com.project.nuroq.android.app.TimeAlarm" />

我想在特定时间(已实施)和日期(尚未实施)发出通知,这是现在没有发生的。最后一个只是被解雇了!

我哪里错了?提前谢谢!

1 个答案:

答案 0 :(得分:1)

您必须为每个通知使用不同的ID。如果像你一样,所有人都使用相同的ID值,那么,根据notify() docs,前一个会被新的替换:

  

发布要在状态栏中显示的通知。如果是通知   具有相同ID的已经由您的应用程序发布并具有   尚未取消,将被更新的信息取代。