1
我想讓兩個鬧鐘啓動他們自己的單獨服務。它們似乎互相干擾,因爲一次只有其中一個工作。有什麼我應該改變我的代碼?或者是不可能做到這一點?如何讓兩個警報啓動兩項服務?
public void setAlarm(){
AlarmManager alarmMgr1 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, DrawOverAppsService.class);
Float alpha2 = alpha.getProgress()/100f;
intent.putExtra("alpha", alpha2);
PendingIntent alarmIntent1 = PendingIntent.getService(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if(start.isChecked()) {
alarmMgr1.cancel(alarmIntent1);
java.util.Calendar calendar = java.util.Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(java.util.Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
alarmMgr1.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent1);
Log.d("a","b");
}
if(start.isChecked() == false) {
alarmMgr1.cancel(alarmIntent1);
}
}
public void stopAlarm(){
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent5 = new Intent(this, stopOverlay.class);
PendingIntent alarmIntent = PendingIntent.getService(MainActivity.this, 2, intent5, 0);
if(stop.isChecked()) {
alarmMgr.cancel(alarmIntent);
java.util.Calendar calendar = java.util.Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(java.util.Calendar.HOUR_OF_DAY, 5);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
}
if(stop.isChecked() == false) {
alarmMgr.cancel(alarmIntent);
}
}