Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7494637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:41:25+00:00 2026-05-29T17:41:25+00:00

My application has only one service class (not activity) in which I keep value

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T17:41:26+00:00Added an answer on May 29, 2026 at 5:41 pm

    the quickest way is to save your variable’s value in SharedPreferences and retrieve it for later use.

    For example:

    public class Calc extends Activity {
        public static final String PREFS_NAME = "MyPrefsFile";
    
        @Override
        protected void onCreate(Bundle state){
           super.onCreate(state);
           . . .
    
           // Restore preferences
           SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
           boolean silent = settings.getBoolean("silentMode", false);
           setSilent(silent);
        }
    
        @Override
        protected void onStop(){
           super.onStop();
    
          // We need an Editor object to make preference changes.
          // All objects are from android.context.Context
          SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
          SharedPreferences.Editor editor = settings.edit();
          editor.putBoolean("silentMode", mSilentMode);
    
          // Commit the edits!
          editor.commit();
        }
    }
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on an application that has a read-only database shipped with it. The
I have an application that seems to throw exceptions only after the program has
I have a WPF application which so far has been client only, but now
This application has a WCF web service that is called by a WinForms app.
I have an application that is started by a service. The application has no
I am trying to reuse one code in ios application . That code has
Assume i have a custom Windows application written in C#. This application has only
My application has a simple data model with 70 read-only records grouped into six
My application has an MVC structure. Is it sufficient to only cache the model
I maintain an old PC-only application written in Delphi 7. Although Delphi has served

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.