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

  • Home
  • SEARCH
  • 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 8211499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:26:46+00:00 2026-06-07T10:26:46+00:00

stackoverflow community. I have a problem that I’ve been trying to solve since a

  • 0

stackoverflow community.

I have a problem that I’ve been trying to solve since a few days ago.

I have a service running in an Android application that receives and sends messages. For those messages, I have counters that save the ammount of sent and received sms and errors. For example, when a message is sent, the counter adds +1 and shows the total on screen (in a not focusable editview).

Another thing I do is bind my service, so I can switch it on or off according to my needs. Everything works fine when the application is running, but if it is finished my variables are lost, but I need to keep counting the messages somehow and show the values when the user opens the application again. How can I do this?

I’ve been using shared preferences variables to save these numbers and others configurations, but when the app is killed, those numbers can’t keep increasing.

When the application is closed, can I save the counters values somewhere? Can I access to the shared preferences from the service with the application closed? What if I use a content provider? Can I access to it from the running service even if the app is closed?

Regards.

  • 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-06-07T10:26:47+00:00Added an answer on June 7, 2026 at 10:26 am

    There is no problem accessing shared preferences from a service. So, just save your variables on your services onDestroy() method and reload them onCreate();

    This should work to get the preferences:

    Context ctx = getApplicationContext();
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    

    Here’s a great answer: How do I get the SharedPreferences from a PreferenceActivity in Android?

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

Sidebar

Related Questions

Hello StackOverflow community, I have run into a problem that quite frankly is baffling
Dear stackoverflow community, I have a simple menu that have to be dynamics. With
Hello stackoverflow community ! I am trying to figure out how to architect my
Hi everybody of the stackoverflow community! I've been visiting this site for years and
Recently, the StackOverflow community helped me develop a fairly concise @memoize decorator that is
First, I just want to say that the stackoverflow community is empowering to say
Good day, to stackoverflow community. I have run into a bug with regard to
Houston, I have a problem, which I am working on for 5 days and
You may have noticed that we now show an edit summary on Community Wiki
We have been trying to use virtual machines for build servers. Our build servers

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.