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. 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!
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.
Accessing hidden method Landroid/app/LoadedApk;->rewriteRValues(Ljava/lang/ClassLoader;Ljava/lang/String;I)V (greylist, linking, allowed)
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.