2017-04-01 57 views
0

我是新到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); 
} 

請任何一個清楚我的疑問......等待着您的積極響應......

回答

0

你需要停止報警。播放後嘗試ringtone.stop()

+0

:(不wroking ......同樣的問題... – Manish

+0

alarmIntent = PendingIntent.getBroadcast(背景下,0,意圖的PendingIntent .FLAG_CANCEL_CURRENT); –

+0

試試這個要麼工作或沒有 –