Reputation: 970
Explanation: In my application token will change after every 24 hours.so, i want to store old data for 23 hour in sharedpreferences ok i did it. Now if more than 23 hours passed my new authentication token will replace my new data in sharedpreference. It means my token will work for next 23 hour.
How can i check my 23 hours passed from my first authentication time?
Here is my code to get the token and save into sharedpreference
private void Authentication_App_Service(String responseStr) {
int hours=0;
int minutes=0;
try {
Calendar c = Calendar.getInstance();
hours = c.get(Calendar.HOUR_OF_DAY);
Log.e("HOUR OF DAY",""+hours);
minutes = c.get(Calendar.MINUTE);
}
catch (Exception e){
e.printStackTrace();
}
try {
JSONObject jobj_res = new JSONObject(responseStr);
String status = jobj_res.getString("status");
String status_code = jobj_res.getString("status_code");
statusCode = Integer.parseInt(status_code);
if (status.equals("True") || status.equals("true")) {
JSONObject a_res = jobj_res.getJSONObject("auth");
main_public_access_token = a_res.getString("access_token");
SharedPreferences sharedTimer=this.getSharedPreferences("SaveTime",Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedTimer.edit();
editor.putString("accessToken", main_public_access_token);
editor.putInt("authentication_time", hours);
editor.commit();
} else {
dialog_popup();
}
} catch (JSONException e) {
e.printStackTrace();
}
Log.e("checking value >> ", ""+ main_public_access_token);
if(main_public_access_token.equals("")){
dialog_popup();
} else {
Intent i = new Intent(getBaseContext(),
MainActivity.class);
i.putExtra(key_access_token, main_public_access_token);
startActivity(i);
finish();
}
}
Above method will run at every 23 hour.Before 23 hour this method will not called again.
I use my token from sharedpreference before 23 hours. Please help me to solve out this problem.
Upvotes: 1
Views: 872
Reputation: 1039
To repeat the same task after some time use Timer with simple & sorted method
private TimerTask doAsynchronousTask;
public void callAsynchronousTask() {
//final Handler handler = new Handler();
Timer timer = new Timer();
doAsynchronousTask = new TimerTask() {
public void run() {
try {
// Write Your code here
} catch (Exception e) {
e.printStackTrace();
}
}
};
timer.schedule(doAsynchronousTask, 0, (23*60*60*1000)); //execute in every 23 Hrs
}
Upvotes: 0
Reputation: 2200
Use AlarmManager for this.Set it repeating for every 23 hours. Use following code
//Create a new PendingIntent and add it to the AlarmManager
Intent intent = new Intent(this, RingAlarm.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am =
(AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),
23 * 60 * 60 * 1000,pendingIntent);
Above written code will fire an intent after every 23 hours.Let me know if you want more explanation.Mark this up if it helps.
Upvotes: 3