我正在使用警報管理器調用 REST API。但問題是當用戶更改系統(tǒng)時鐘時間時它會立即觸發(fā)。必須在上午 9:00 準確調用 REST API。假設用戶在上午 8:00 更改時鐘,并且鬧鐘在上午 10:00 觸發(fā),因此會導致問題。我使用 PHP-MySQL 作為后端。你也可以說我的鬧鐘不必依賴于Android系統(tǒng)時間。它必須取決于印度標準時間。應在上午 9:00 除外調用 REST API后端 PHPREST API 恰好在上午 9:00 調用。當用戶更改系統(tǒng)時鐘時間時,警報管理器會立即觸發(fā)。設置報警方式 public static void setAlarm(Context context) { Intent intent = new Intent(context, MyBroadcastReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 234324243, intent, 0); AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY, 9); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); if (calendar.before(Calendar.getInstance())) { calendar.add(Calendar.DATE, 1); } alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); }BroadcastReceiver.java public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (isNetworkConnected(context)) { doUpdate(context); } } private void doUpdate(final Context context) { class DoInsert extends AsyncTask<String, String, String> { @Override protected String doInBackground(String... strings) { Retrofit retrofit = AppConfig.getRetrofit(); Api service = retrofit.create(Api.class); Call<Response> call = service.doOperation(); call.enqueue(new Callback<Response>() { @Override public void onResponse(Call<Response> call, retrofit2.Response<Response> response) { Response response1 = response.body(); Log.d("RESPONSE ======== ", response1.getMessage()); return; }
Android (JAVA) - 每天上午 9:00 在特定時間調用 REST API
30秒到達戰(zhàn)場
2023-07-15 18:36:44
