Android (JAVA) - 每天上午 9:00 在特定时间调用 REST API

我正在使用警报管理器调用 REST API。但问题是当用户更改系统时钟时间时它会立即触发。必须在上午 9:00 准确调用 REST API。假设用户在上午 8:00 更改时钟,并且闹钟在上午 10:00 触发,因此会导致问题。我使用 PHP-MySQL 作为后端。

你也可以说我的闹钟不必依赖于Android系统时间。它必须取决于印度标准时间。

应在上午 9:00 除外调用 REST API

  1. 后端 PHP

  2. REST API 恰好在上午 9:00 调用。

  3. 当用户更改系统时钟时间时,警报管理器会立即触发。

设置报警方式

   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;

                }

30秒到达战场
浏览 130回答 1
1回答

largeQ

经过很多临时技巧后,我找到了解决方案。警报管理器将占用系统时间,为了避免它,我们可以使用 UTC 时间,但我不知道如何使用它。所以,我通过在php中使用Cron Job解决了这个问题。Web 托管提供了 Cron 作业,它实际上适合这种情况,因为授予用户修改 REST API 的权限是相当危险的。最后感谢所有花时间回答我的问题的人。
打开App,查看更多内容
随时随地看视频慕课网APP