Android 多个通知

Android 多个通知,android,Android,我正在android 2.1中开发任务管理器 我想设置多个通知警报,但在我的程序中,当第二个通知显示时,前一个通知将被清除。请帮助我显示所有通知,如间隔2分钟后假设..完整。。。 我的代码 1) 主要活动 public class AlarmActivity extends Activity { private static final String TAG = "SomeApp "; protected Toast mToast; @SuppressWarnings(

我正在android 2.1中开发任务管理器 我想设置多个通知警报,但在我的程序中,当第二个通知显示时,前一个通知将被清除。请帮助我显示所有通知,如间隔2分钟后假设..完整。。。 我的代码 1) 主要活动

public class AlarmActivity extends Activity {
    private static final String TAG = "SomeApp ";
    protected Toast mToast;

    @SuppressWarnings("unused")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Calendar cal = Calendar.getInstance(); // for using this you need to
                                                // import java.util.Calendar;

        // add minutes to the calendar object
        cal.set(Calendar.HOUR_OF_DAY, 12);
        cal.set(Calendar.MINUTE, 15);

        // cal.set(Calendar.DAY_OF_MONTH, 24);
        // cal.set(Calendar.MONTH,10);
        // cal.set(Calendar.YEAR, 2011);

        // cal.set will set the alarm to trigger exactly at: 21:43, 5 May 2011
        // if you want to trigger the alarm after let's say 5 minutes after is
        // activated you need to put

        Intent alarmintent = new Intent(getApplicationContext(),
                AlarmReceiver.class);
        alarmintent.putExtra("title", "Title of ");
        alarmintent.putExtra("note", "Description of our  Notification");
        // HELLO_ID is a static variable that must be initialised at the
        // BEGINNING OF CLASS with 1;

        int HELLO_ID = 1;
        PendingIntent sender = PendingIntent.getBroadcast(
                getApplicationContext(), HELLO_ID, alarmintent,
                PendingIntent.FLAG_UPDATE_CURRENT | 

                  Intent.FILL_IN_DATA);

        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

        Calendar cal1 = Calendar.getInstance(); // for using this you need to


                cal1.set(Calendar.HOUR_OF_DAY, 12);
        cal1.set(Calendar.MINUTE, 17);

        // cal.set(Calendar.DAY_OF_MONTH, 24);
        // cal.set(Calendar.MONTH,10);
        // cal.set(Calendar.YEAR, 2011);



        Intent alarmintent1 = new Intent(getApplicationContext(),
                AlarmReceiver.class);
        alarmintent1.putExtra("title", "Title 2 ");
        alarmintent1.putExtra("note", "Description 2");


        int HELLO_ID1 = 2;
        PendingIntent sender1 = PendingIntent.getBroadcast(
                getApplicationContext(), HELLO_ID1, alarmintent1,
                PendingIntent.FLAG_UPDATE_CURRENT | 
             Intent.FILL_IN_DATA);  

        AlarmManager am1 = (AlarmManager) getSystemService(ALARM_SERVICE);
        am1.set(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(), sender1);
    }
2) 报警接收器类

public class AlarmReceiver extends BroadcastReceiver {

    private static final String NotificationManager = null;
    private static int NOTIFICATION_ID = 0;

    @Override
    public void onReceive(Context context, Intent intent) {

        // NotificationManager mNotificationManager =

        // (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationManager manger = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.ic_launcher,
                "Combi Note", System.currentTimeMillis());
        PendingIntent contentIntent = PendingIntent.getActivity(context,
                NOTIFICATION_ID, new Intent(context, 
                  AlarmReceiver.class), 0); 

                   Bundle extras = intent.getExtras();

        String title = extras.getString("title");
        // here we get the title and description of our Notification
        //
        String note = extras.getString("note");
        notification.setLatestEventInfo(context, note, title, contentIntent);
        notification.flags = Notification.FLAG_INSISTENT;
        notification.defaults |= Notification.DEFAULT_SOUND;
        // here we set the default sound for our
        // notification

        // The PendingIntent to launch our activity if the user selects this
        // notification
        manger.notify(NOTIFICATION_ID, notification);

    }

};

如果您使用相同的通知id,它们将相互覆盖。

如果您使用相同的通知id,它们将相互覆盖。

谢谢@gwa…它可以工作,但当我按“清除”选项卡时,所有通知都会被清除。是否有办法清除我所看到的通知,并在我忙时让其他人阅读……没问题prakash_d22!您可以使用flag_NO_CLEAR(notification.flags |=notification.flag_NO_CLEAR;)来标记它们,但是您必须使用mNotificationManager.cancel(MY_notification_ID)清除您显式单击的通知;在您登录的代码中…我添加了我刚才提到的标志,然后查看此示例以了解如何清除通知:希望它会有所帮助!谢谢@gwa…它可以工作,但当我按clear tab时,所有通知都会被清除。如果我忙的话,是否可以清除我所收到的通知并让其他人在事后阅读……没问题,prakash_d22!您可以使用flag_NO_CLEAR(notification.flags |=notification.flag_NO_CLEAR;)来标记它们,但是您必须使用mNotificationManager.cancel(MY_notification_ID)清除您显式单击的通知;在您登录的代码中…我添加了我刚才提到的标志,然后查看此示例以了解如何清除通知:希望它会有所帮助!