【问题标题】:run code in android app every 5 minutes when is on background? [duplicate]在后台时每5分钟在android应用程序中运行一次代码? [复制]
【发布时间】:2017-12-18 16:16:27
【问题描述】:

即使应用在后台,我也需要每 5 分钟在 Android 应用中运行一次代码。 我对 android studio 了解不多,比如把代码放在哪里,以及如何正确地做以避免在后台崩溃和不同的错误。

该应用程序是一个 webview,它从站点代码中的 JS 中提取和 Id,然后我需要每 5 分钟运行一次具有该 ID 的脚本。

【问题讨论】:

标签: javascript java android webview


【解决方案1】:

您可以使用AlarmManagerBroadcastReceiver 来完成这项工作。

Intent intent = new Intent(this, YourBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5*60*1000, pendingIntent);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();`

【讨论】:

  • 我必须把这段代码放在哪里?在主要活动中?...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-20
  • 2013-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-05
  • 1970-01-01
相关资源
最近更新 更多