我正在使用警報管理器調用 REST API。但問題是當用戶更改系統時鐘時間時它會立即觸發。必須在上午 9:00 準確調用 REST API。假設用戶在上午 8:00 更改時鐘,并且鬧鐘在上午 10:00 觸發,因此會導致問題。我使用 PHP-MySQL 作為后端。你也可以說我的鬧鐘不必依賴于Android系統時間。它必須取決于印度標準時間。應在上午 9:00 除外調用 REST API后端 PHPREST API 恰好在上午 9:00 調用。當用戶更改系統時鐘時間時,警報管理器會立即觸發。設置報警方式 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; }
1 回答

largeQ
TA貢獻2039條經驗 獲得超8個贊
經過很多臨時技巧后,我找到了解決方案。
警報管理器將占用系統時間,為了避免它,我們可以使用 UTC 時間,但我不知道如何使用它。
所以,我通過在php中使用Cron Job解決了這個問題。Web 托管提供了 Cron 作業,它實際上適合這種情況,因為授予用戶修改 REST API 的權限是相當危險的。
最后感謝所有花時間回答我的問題的人。
- 1 回答
- 0 關注
- 146 瀏覽
添加回答
舉報
0/150
提交
取消