我正在使用警報(bào)管理器調(diào)用 REST API。但問題是當(dāng)用戶更改系統(tǒng)時(shí)鐘時(shí)間時(shí)它會(huì)立即觸發(fā)。必須在上午 9:00 準(zhǔn)確調(diào)用 REST API。假設(shè)用戶在上午 8:00 更改時(shí)鐘,并且鬧鐘在上午 10:00 觸發(fā),因此會(huì)導(dǎo)致問題。我使用 PHP-MySQL 作為后端。你也可以說我的鬧鐘不必依賴于Android系統(tǒng)時(shí)間。它必須取決于印度標(biāo)準(zhǔn)時(shí)間。應(yīng)在上午 9:00 除外調(diào)用 REST API后端 PHPREST API 恰好在上午 9:00 調(diào)用。當(dāng)用戶更改系統(tǒng)時(shí)鐘時(shí)間時(shí),警報(bào)管理器會(huì)立即觸發(fā)。設(shè)置報(bào)警方式 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 在特定時(shí)間調(diào)用 REST API
30秒到達(dá)戰(zhàn)場
2023-07-15 18:36:44