怎么设置闹钟和通知?

时间:2011-09-05 06:24:25

标签: android alarmmanager alarm android-alarms

我一直在做我的闹钟项目..

我的代码是这样的:

    String a = interval.getText().toString();

   Intent myIntent = new Intent(AndroidAlarmService.this, MyAlarmService.class);
   pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0);

            AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.add(Calendar.SECOND, 10);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), Integer.parseInt(a)*1000, pendingIntent);

            NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

            int icon = R.drawable.alarm_icon;
            CharSequence tickerText = "Hello";
            long when = System.currentTimeMillis();

            Notification notification = new Notification(icon, tickerText, when);

            Context context = getApplicationContext();
            CharSequence contentTitle = "My notification";
            CharSequence contentText = "Hello World!";
            Intent notificationIntent = new Intent(AndroidAlarmService.this, AndroidAlarmService.class);
            PendingIntent contentIntent = PendingIntent.getActivity(AndroidAlarmService.this.getBaseContext(), 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

            notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

            final int HELLO_ID = 1;

            mNotificationManager.notify(HELLO_ID, notification);

   Toast.makeText(AndroidAlarmService.this, "Start Alarm", Toast.LENGTH_LONG).show();
  }});

我遇到了问题,通知总是出现一次,不会重复出现。

我想设置一个可重复的警报以及通知,所以每次警报激活时,通知也会出来......例如,当我每隔1小时设置一次警报时,每隔一小时就会发出一次警报触发器和通知也会出来..

谁能帮帮我?

之前谢谢

1 个答案:

答案 0 :(得分:0)

在服务类中传递您的通知,即在onStartCommand()