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 7697833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:06:07+00:00 2026-05-31T22:06:07+00:00

I have a service that performs background updates. I want to give the user

  • 0

I have a service that performs background updates.

I want to give the user the the option to disable the updates when their battery percentage reaches a certain level.

From my research, I’m going to use a receiver in the onCreate method of my Service class, eg:

 public class MainService extends Service  
 {  
     @Override
     public void onCreate()
     {      
        this.registerReceiver(this.BatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
     }

    private BroadcastReceiver BatInfoReceiver = new BroadcastReceiver(){  
         @Override  
         public void onReceive(Context arg0, Intent intent) {  
           int level = intent.getIntExtra("level", 0);  
    }  
  };
 }

I’m assuming that the best practice is to leave the service running, check the battery level in the service, and not perform the CPU-intensive code, based on the percentage?

I don’t actually need to stop the service itself and start it up again, based on the battery percentage?

UPDATE:

This seems to be a better solution, but not 100% sure. I registered a BroadcastReceiver in the AndroidManifest:

<receiver android:name="BatteryReceiver" /> 

Then created a BroadcastReceiver:

 public class BatteryReceiver extends BroadcastReceiver 
 {  
     @Override
     public void onReceive(final Context context, final Intent intent) 
     {      
         final int currentBatteryPercent = intent.getIntExtra("level", 0);
        final int disableBatteryPercent = Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(context).getString("batteryPercent", 0);

        //AlarmReceiver is the service that performs the background updates
         final ComponentName component = new ComponentName(context, AlarmReceiver.class);

         if (currentBatteryPercent < disableBatteryPercent)
         {
             context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED , PackageManager.DONT_KILL_APP);
         }
         else
         {
             context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_ENABLED , PackageManager.DONT_KILL_APP);
         }
      }
 }
  • 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-31T22:06:09+00:00Added an answer on May 31, 2026 at 10:06 pm

    That’s right. What you will typically do is schedule some broadcast intent for an update (perhaps through an AlarmManager). When you get the notification that there is a low battery, you will stow this away in your service, and then before doing an update, check to ensure that the battery isn’t too low. here is a good tutorial of watching the battery level. You shouldn’t do much when handling this intent, just stick the battery level somewhere and (before doing an update) make sure it’s appropriately “full.”

    Your update is a really bad way to stop an app. In general, asking the package manager to stop your component is much more of a hack than a solution. Instead, you should move some code into the component that actually does the updating, and store / update the information for the battery info there. Before you do an update, you check the battery level and don’t proceed unless it’s at a level where you feel comfortable updating the app. Using a broadcast receiver is fine, but you need to store the level somewhere (perhaps a sharedprefs). Instead, this is why putting the receiver within the service where you do the updating is probably best.

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

Sidebar

Related Questions

I have a repeating AlarmManager that calls a Service to perform some background updates.
I have a comprehension question about Android Services. I have a Service that performs
I have a fairly straightforward WCF service that performs one-way file synchronization for a
Scenario I have a windows service written in C# that performs some processing based
I have a Windows Service that performs a number of periodic activities, and I
I have a workflow inside a Windows Service that is a loop that performs
I have a Windows service that continuously performs calculation tasks. There are multiple calculation
Background I have a Windows service that uses various third-party DLLs to perform work
Background I have a service that is connecting to Microsoft CRM 2011, and updating
Background: I have a windows service that works in the background (obviously), and uses

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.