ユーザーが最初に日とタイミングを含む単純な UI を表示するアプリケーションを開発しています。ユーザーは、毎日に対してタイミングを選択します。「完了」ボタンをクリックします。これで、アラーム サービスが開始され、選択したタイミングに従って毎日トリガーされます。(そして、これらのタイミングでBluetoothデバイスをオフ/オンにします)。現在、私は毎日別々のサービスを使用しています(初心者の本能)。アプリケーションは正常に動作しています。今私が欲しいのは、ユーザーが「完了」ボタンをクリックしたときにアプリケーションがバックグラウンドで実行され続け、ユーザーがアプリケーションアイコンを再度クリックして「デフォルト」ボタンをクリックすると、すべてのサービスを停止することです。どうすればそれを達成できますか? 毎週日曜日にサービスをトリガーするコードは次のとおりです。
time interval of 7 x days for the alarm to repeat every 7 days
long interval = 1000 * 60 * 60 * 24 * 7; // to make the alarm repeat at every 7 days
//getting values for hours, mins and AM/PM from the spinner boxes for sunday
index = sunHr.getSelectedItemPosition();
int sunHrInt = Integer.parseInt(hrList[index]);
index = spinnerSunMin.getSelectedItemPosition();
int sunMinInt = Integer.parseInt(minList[index]);
index = spinnerSunAmPm.getSelectedItemPosition();
//conversion of time to 24 hrs format
if (ampmList[index] == "AM") //(convert to 24 hr format)
{
if (sunHrInt == 12)
{
sunHrInt = 0;
}
else
{
if (sunHrInt != 12)
sunHrInt = sunHrInt + 12;
}
}
//setting current calender
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar
//setting calender for sunday
Calendar calSun = new GregorianCalendar();
calSun.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
calSun.set(Calendar.HOUR_OF_DAY, sunHrInt);
calSun.set(Calendar.MINUTE,sunMinInt);
calSun.set(Calendar.SECOND, 0);
calSun.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
calSun.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
calSun.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
//finding out when the sunday is to occur from today
days = 8 - calSun.get(Calendar.DAY_OF_WEEK); // how many days until Sunday
if (days >= 7)
{
days = days - 7;
}
calSun.add(Calendar.DATE, days);
//finally triggering the intent
Intent myIntentSun = new Intent(AndroidAlarmService.this, SunOffAlarmService.class);
pendingIntentSun = PendingIntent.getService(AndroidAlarmService.this, 0, myIntentSun, 0);
AlarmManager alarmManagerSun = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManagerSun.set(AlarmManager.RTC_WAKEUP, calSun.getTimeInMillis(), pendingIntentSun);