2017-04-03 127 views
0
public class setAlarm extends AppCompatActivity { 

final Calendar setat = Calendar.getInstance(); 
Calendar[] calendar = new Calendar[8]; 

public void dimensions(){ 
    DisplayMetrics dm = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(dm); 

    int width = dm.widthPixels; 
    int height = dm.heightPixels; 

    getWindow().setLayout((int) (width * 0.62), (int) (height * 0.4)); 
} 

public void clock(){ 
    final TextView ora = (TextView)findViewById(R.id.clock); 
    findViewById(R.id.clock).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      final Calendar time = Calendar.getInstance(); 
      int hour = time.get(Calendar.HOUR_OF_DAY); 
      int minute = time.get(Calendar.MINUTE); 

      TimePickerDialog timePickerDialog; 
      timePickerDialog = new TimePickerDialog(setAlarm.this, new TimePickerDialog.OnTimeSetListener(){ 
       @Override 
       public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute){ 
        ora.setText(selectedHour + ":" + selectedMinute); 
        setat.set(Calendar.HOUR_OF_DAY, selectedHour); 
        setat.set(Calendar.MINUTE, selectedMinute); 
       } 
      }, hour, minute, true); 
      timePickerDialog.setTitle("Select Time"); 
      timePickerDialog.show(); 
     } 
    }); 
} 

public void alarm(){ 
    clock(); 
    findViewById(R.id.confirm).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      CheckBox[] boxes = new CheckBox[8]; 
      boxes[0] = (CheckBox)findViewById(R.id.check0); 
      boxes[1] = (CheckBox)findViewById(R.id.check1); 
      boxes[2] = (CheckBox)findViewById(R.id.check2); 
      boxes[3] = (CheckBox)findViewById(R.id.check3); 
      boxes[4] = (CheckBox)findViewById(R.id.check4); 
      boxes[5] = (CheckBox)findViewById(R.id.check5); 
      boxes[6] = (CheckBox)findViewById(R.id.check6); 

      if(boxes[0].isChecked()) {setAlarm(1);} 
      if(boxes[1].isChecked()) {setAlarm(2);} 
      if(boxes[2].isChecked()) {setAlarm(3);} 
      if(boxes[3].isChecked()) {setAlarm(4);} 
      if(boxes[4].isChecked()) {setAlarm(5);} 
      if(boxes[5].isChecked()) {setAlarm(6);} 
      if(boxes[6].isChecked()) {setAlarm(7);} 

      Toast.makeText(setAlarm.this, "Alarm has been set!", Toast.LENGTH_SHORT).show(); 
      //finish(); 
     } 
    }); 

} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.alarm); 
    dimensions(); 
    alarm(); 
} 

public void setAlarm(int day){ 
    Calendar calendar = Calendar.getInstance(); 
    calendar.set(Calendar.DAY_OF_WEEK, day); 
    calendar.set(Calendar.HOUR_OF_DAY, setat.get(Calendar.HOUR_OF_DAY)); 
    calendar.set(Calendar.MINUTE, setat.get(Calendar.MINUTE)); 
    calendar.set(Calendar.SECOND, 0); 

    long time = calendar.getTimeInMillis() - Calendar.getInstance().getTimeInMillis(); 

    Log.d("day", String.valueOf(day)); 
    Log.d("time", String.valueOf(setat.get(Calendar.HOUR_OF_DAY)) + ":" + String.valueOf(setat.get(Calendar.MINUTE))); 
    Log.d("actual", String.valueOf(Calendar.getInstance().get(Calendar.MINUTE))); 

    Intent intent = new Intent(getApplicationContext(), NotificationService.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

    AlarmManager alarmManager = (AlarmManager)getApplicationContext().getSystemService(ALARM_SERVICE); 

    alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 
} 
} 

我想在指定的時間和分鐘設置鬧鐘,並且應該在選定日期每週重複該鬧鐘。問題在於,在確認報警後立即發出警報,或者不在選定的時間。我之前曾經搜過這個問題,但是我找不到對我有用的東西。即時觸發Android鬧鐘

回答

0

如果警報立即發射,那麼問題可能是時間在過去,而不是未來。您應該嘗試確保(在您的alarm方法中)calendar.getTimeInMillis()大於System.currentTimeMillis(),並且如果不是,則適當調整日曆實例。

+0

如果'System.currentTimeMillis()'大於'calendar.getTimeInMillis()',我該怎麼辦? –

+0

這將取決於您需要的具體時間:在我的應用程序中,我的鬧鐘是每天都做的,所以如果我的計算在過去給了我一個數字,我只需要以毫秒爲單位添加1天,直到將來使用它。 – Femi