My application has only one service class (not activity) in which I keep value of one variable that is being dynamically calculated through one method. Then i need to save that value for further comparing.
For example:
class MyClass extends Service{
static int number1;
private void Method(){
int number 2;
/// some calculations for number2 ///
if number2 != number1 { number1 = number2 }
}
I need to compare that variable (number1) to the some other variable (number2) that my application has calculated. This is working well, but when Android OS kills my service, and start that service on some intent (let say after phone reboot) the value of number1 is being lost and I can’t use it for comparing.
So how to do it? I thought to write simple TXT file in which one i could keep value of number1 after killing the service,
is there any other way?
the quickest way is to save your variable’s value in SharedPreferences and retrieve it for later use.
For example:
this code is taken from android developer’s page which is for Activity but you may adapt it in your service easily by changing it