2011-11-26 56 views
0

我正在开发中的Android 2.1 我想设置多个通知警报,但在我的计划任务管理器时,第二通知被显示前一个被清除.plz帮助我,以显示所有通知的间隔2分钟后,说想..as完整... 我的代码 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); 

    } 

}; 

回答

1

如果使用相同的通知ID他们的Wi会相互覆盖。

+0

谢谢@gwa ....它的作品,但是当我按下清除选项卡所有notificatins得到清除..isit任何方式来清除通知,我widh并保持oters后来如果我很忙...... –

+0

没问题prakash_d22!您可以与FLAG_NO_CLEAR(notification.flags | = Notification.FLAG_NO_CLEAR;)标志他们,但那么你必须明确的是,你点击明确使用mNotificationManager.cancel(MY_NOTIFICATION_ID)的通知;在你的土地上的代码... – gwa

+0

没有得到you..plzü可以在我的代码编辑和repaste它.... –