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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:57:23+00:00 2026-06-06T20:57:23+00:00

I am able to listen for the Intent Intent.ACTION_BATTERY_CHANGED within my activity and it

  • 0

I am able to listen for the Intent Intent.ACTION_BATTERY_CHANGED within my activity and it works great. I tried defining the receiver in the manifest:

<receiver android:name="com.beargreaves.battery.BatteryReceiver"> 
    <intent-filter> 
        <action android:name="android.intent.action.BATTERY_CHANGED" />
    </intent-filter> 
</receiver>

but it didn’t work, I read a post on here saying that it must be defined programmatically. Rather than registering for the receiver in my Activity, I wanted to achieve this in a Service so that it continually monitors. I have successfully achieved this, and it works, but I wanted to check my working to see if it is the correct approach.

I started by extending BroadcastReceiver:

public class BatteryReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();

        if(null == bundle)
            return;

        boolean isPresent = intent.getBooleanExtra("present", false);
        String technology = intent.getStringExtra("technology");
        int plugged = intent.getIntExtra("plugged", -1);
        int scale = intent.getIntExtra("scale", -1);
        int health = intent.getIntExtra("health", 0);
        int status = intent.getIntExtra("status", 0);
        int rawlevel = intent.getIntExtra("level", -1);
        int level = 0;

        Log.d("Debug","Battery Receiver OnReceive");

        if(isPresent) {
            if (rawlevel >= 0 && scale > 0) {
                level = (rawlevel * 100) / scale;

                Log.d("Debug","BatterReceiver: " + level);

                Toast.makeText(context,"Battery Receiver: " + level + "\t" + status + "Raw: " + rawlevel,Toast.LENGTH_LONG).show();

                if(level <60) {
                    /*
                     * Only invoke the service when level below threshold
                     */
                    Intent i = new Intent(context, BatteryService.class);
                    i.putExtra("level", level);
                    context.startService(i);
                }
            }
        }
    }
}

And then I use a Service to first register the receiver in onCreate() and then handle the events in onStart(). My BroadcastReceiver starts the Service if the level is below a threshold.

public class BatteryService extends Service {
    /*
     * First Call to onStart we don't want to do anything
     */
    boolean avoidFirst = false;
    private BroadcastReceiver receiver;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        receiver = new BatteryReceiver();
        registerReceiver(receiver, filter);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        Log.d("Debug","Battery Service On Start");
        int level = intent.getIntExtra("level", -1);

        if(avoidFirst) {
            if(level != -1) {
                Log.d("Debug","Battery Alert Notifying..... " + level);
                SMSManager.sendSMS(BatteryService.this, "<number redacted>", "Battery Level Aleart: " + level);
                stopSelf();
            }
        } else {
            avoidFirst = true;
        }
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        PreferenceUtil.updatePreference(BatteryService.this, "battery_monitor_on", false);
        unregisterReceiver(receiver);
    }
}

Is this the correct approach? Be that register the receiver in onCreate() and then start the Service when an event is received. First I tried not using a Service but then I have no way of registering the receiver since it can’t be achieved in the manifest. Secondly an exception was thrown when I tried to send a text message in the onReceive(). I read that onReceive() shouldn’t be starting any threads.

Thanks in advance

  • 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-06T20:57:25+00:00Added an answer on June 6, 2026 at 8:57 pm

    Yes, that is the correct approach. It’s quite similar to what I do with my app, which has been working well in production for over a decade (since Android’s early days), and I don’t see anything that you’re missing or getting wrong here. You’re correct that you can’t register for that broadcast from your Manifest; you do need to do so programmatically. Based on the rest of what you’ve shared, doing that in a Service rather than an Activity is clearly correct.

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

Sidebar

Related Questions

I added this is my manifest file - <receiver android:name=.ServiceReceiver> <intent-filter> <action android:name=android.intent.action.PHONE_STATE />
I tried posting this on the Android Developers list and didn't hear anything -
I am able to get the package name of apps that were removed by
I want to be able to listen to all events dispatched on the Event
The application I am working on should be able to listen to multiple (right
I need to write simple TCP listener that will be able to listen TCP
I am using JMF jar file, but i am only able to listen the
I want to listen to touch events for a viewFlipper. I've been able to
I have a somewhat weird requirement to be able to listen to a number
I am playing around with Android's speech recognition and would like to be able

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.