警报不断响起?

时间:2017-04-01 15:18:18

标签: java android alarmmanager

我是Android的新手,这是我的代码,我在sqlite中存储了警报值,警报响了正确的时间,但我的警报响了不停......

     private void night() {
    DatabaseHelper db = new DatabaseHelper(getContext());
    ls = db.getAllinfo();
    for (int i = 0; i < ls.size(); i++) {
        Calendar c = Calendar.getInstance();
            int hour = Integer.parseInt(ls.get(i).getSch_night_hour());
            int mins = Integer.parseInt(ls.get(i).getSch_night_mins());
        //int hour=20;
        //int mins=06;
            int cr_hour = c.get(Calendar.HOUR_OF_DAY);
            int cr_mins = c.get(Calendar.MINUTE);
            if (cr_hour >= hour) {

                if (cr_mins == mins) {
                    try {
                        new AlarmReceiver(cr_hour,mins,10).setAlarm(getActivity());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return;
                } else {

                    Log.e("night", "for hr waiting");

                }
            } else {
                Log.e("night", "for mins waiting");
            }
      }

  return;
}

AlarmReciver.class

   public void setAlarm(Context context) {
        pf=new PrimaryFragment();
        alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, Alarmclass.class);
        alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, min);
    calendar.set(Calendar.SECOND, sec);
       // alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
             //   calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
        alarmMgr.set(AlarmManager.RTC_WAKEUP,
                calendar.getTimeInMillis(), alarmIntent);
        Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);

        vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(800);
        ringtone = RingtoneManager.getRingtone(context, alarmUri);
        ringtone.play();
      //  pf.showstatus();
        //notification
        ComponentName comp = new ComponentName(context.getPackageName(),
                Alarmclass.class.getName());
        startWakefulService(context, (intent.setComponent(comp)));
    alarmMgr.cancel(alarmIntent);
}

请任何人明白我的怀疑......等待你的积极回应...

1 个答案:

答案 0 :(得分:0)

您需要停止闹钟。播放后尝试ringtone.stop()