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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:12:02+00:00 2026-05-26T08:12:02+00:00

I have created a battery widget that uses a Service to listen for Battery

  • 0

I have created a battery widget that uses a Service to listen for Battery changes.
However because of this, when created the widget must wait before the battery state changes before it can display data.

My question is whether or not there is simple method of interrogating the battery as a one off occurrence.

I may be barking up the wrong tree but I have tried using a BroadcastReceiver in the AppWidgetProvider class to get the initial state of the battery but can’t figure out how to stop that so that the service can take it from there.

Any thoughts?

Cheers

  • 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-26T08:12:03+00:00Added an answer on May 26, 2026 at 8:12 am

    I have created a battery widget that uses a Service to listen for Battery changes.

    Note that keeping a service around all of the time is not generally a good idea. Users don’t like it and will kill it off with a task killer or the Manage Services screen in Settings. Android doesn’t like it either and will eventually stop it.

    Since the battery level does not change very frequently, consider switching to a model where you use AlarmManager and check the battery level every few minutes.

    However because of this, when created the widget must wait before the battery state changes before it can display data.

    No. When you register for ACTION_BATTERY_CHANGED, your BroadcastReceiver is immediately invoked with the last-sent broadcast for that action, since ACTION_BATTERY_CHANGED is a sticky broadcast.

    You can see that in this sample project. The battery information is displayed in the activity immediately — that is only possible because the onReceive() method is invoked immediately.

    My question is whether or not there is simple method of interrogating the battery as a one off occurrence.

    That too is possible. Call registerReceiver() for ACTION_BATTERY_CHANGED, but with a null BroadcastReceiver. You will be returned the last-sent broadcast Intent for that action. This would be the way to handle it from the AlarmManager event, for example.

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

Sidebar

Related Questions

I have created a Windows service that will run an exe file periodically to
I have created some JQuery that will expand a div 'popup' on hover and
I have created an android application that calls (using kSOAP library) a SOAP based
I have created a function that shows/hides different messages according to a combination of
I have created a dialog and got a problem that only part of the
I have an image that is 480x270. In theory this should occupy all the
I have created a simple chat server that is driven by client polling. Clients
I've created a service implementing LocationManager. I have a question though. Let's say I
I have created widget position in wordpress theme: <div class=row> <?php if ( dynamic_sidebar('Top'))
I have some intent filters created that work fine but - I only get

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.