显示很多时间的通知

时间:2014-01-11 04:25:26

标签: android notifications alarmmanager

我有一个数组,其格式为日期,如

(String [] ddstart={"2014-1-11 10:45:00 ","2014-1-11 10:46:00 ","2014-1-11 10:47:00 "}).

我想在特定时间显示通知,但在我的项目中,通知显示很多时间。请,建议我该怎么做。 这是我的代码。

 String [] ddstart={"2014-1-11 10:45:00 ","2014-1-11 10:46:00 ","2014-1-11 10:47:00 "};
 SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    for(int i = 0 ; i <  ddstart.length ; i++){
        Date d1 = null;
            Calendar tdy1= Calendar.getInstance();

            try {
                d1 = format.parse(ddstart[i]);
            } catch (java.text.ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            tdy1.setTime(d1);
            long startTime = tdy1.getTimeInMillis();
            Log.i("tdy1 milli",startTime+"");

            startAlarm(startTime);
    }
}

private void startAlarm(long time){
    AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
    Intent myIntent=new Intent(this,timealarm.class);
    final int id= (int) System.currentTimeMillis();
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id, myIntent,PendingIntent.FLAG_ONE_SHOT);
    alarmManager.set(AlarmManager.RTC, time, pendingIntent);
}

这是广播接收器。

 public void onReceive(Context context, Intent paramIntent) {
    // TODO Auto-generated method stub
    Intent myIntent=new Intent(context,service.class);
    context.startService(myIntent);
    Log.i("BroadcastReceiver","receiber hmar");
}

这是服务类。

 public void onStart(Intent intent, int startId)
   {
     super.onStart(intent, startId);

        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        long when = System.currentTimeMillis();         // notification time
        Notification notification = new Notification(R.drawable.ic_launcher, "reminder", when);
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.flags |= notification.FLAG_AUTO_CANCEL;
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent , 0);
        notification.setLatestEventInfo(getApplicationContext(), "It's about time", "You should open the app now", contentIntent);
        nm.notify(NOTIF_ID, notification);
    }

帮助我。

0 个答案:

没有答案