[英]How to call this retrofit method every hour?
How do I call this method every hour since I am passing token value and taking the data from the api I want to get the data and pass it into another method because of that reason I want to call this actualData method every 1 hour or 5 minutes.我如何每小时调用一次此方法,因为我正在传递令牌值并从 api 中获取数据我想获取数据并将其传递给另一个方法,因为这个原因我想每 1 小时或 5 分钟调用一次这个 actualData 方法. I tried Handler but I cannot access the data from the api also I thought of using alarmmanager but I don't have any idea about how to implement it to this retrofit method!
我试过 Handler 但我无法从 api 访问数据我也想过使用 alarmmanager 但我不知道如何将它实现到这个改造方法!
Is there any way?有什么办法吗?
public void actualData(String tokenValue) {
Call<ActualData> call2 = mapiPass.actualData("Bearer " + tokenValue);
call2.enqueue(new Callback<ActualData>() {
@Override
public void onResponse(Call<ActualData> call, Response<ActualData> response) {
if (response.isSuccessful()) {
**data taken from the api**
}
}
}
@Override
public void onFailure(Call<ActualData> call, Throwable t) {
Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_LONG).show();
}
});
}
This is what I have got when I used Handler.这是我在使用 Handler 时得到的。
Accessing hidden method Landroid/app/LoadedApk;->rewriteRValues(Ljava/lang/ClassLoader;Ljava/lang/String;I)V (greylist, linking, allowed)
访问隐藏方法 Landroid/app/LoadedApk;->rewriteRValues(Ljava/lang/ClassLoader;Ljava/lang/String;I)V(灰名单,链接,允许)
This Timer task will run every hour此计时器任务将每小时运行一次
Timer timer = new Timer ();
TimerTask oneHour = new TimerTask () {
@Override
public void run () {
// your code here...
}
};
timer.schedule (oneHour, 0l, 1000*60*60);
If you don't want to use Timer then use Handler or Alarm Manager如果您不想使用 Timer,请使用 Handler 或 Alarm Manager
Declare Global variables:声明全局变量:
Handler handler = new Handler();
Runnable runnable;
int delay = 3600000; //for every hour
Handler:处理程序:
@Override
protected void onResume() {
handler.postDelayed(runnable = new Runnable() {
public void run() {
handler.postDelayed(runnable, delay);
Toast.makeText(MainActivity.this, "This method is run every 10 seconds",
Toast.LENGTH_SHORT).show();
}
}, delay);
super.onResume();
}
@Override
protected void onPause() {
handler.removeCallbacks(runnable); //stop handler when activity not visible super.onPause();
}
You can use worker manager.您可以使用工人管理器。 For Examples, Please follow the documentation.
对于示例,请按照文档进行操作。
https://developer.android.com/topic/libraries/architecture/workmanager/basics https://developer.android.com/topic/libraries/architecture/workmanager/basics
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.