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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:14:20+00:00 2026-06-17T12:14:20+00:00

I am using this code to show battery level in my app. It works

  • 0

I am using this code to show battery level in my app. It works fine but doesn’t update battery level automatically.

What should I do to update the battery level automatically.

   public class MainActivity extends Activity {


        private TextView txtBattery;

        private BroadcastReceiver mBatteryLevelReciver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {

                context.unregisterReceiver(this);
                //int rawLevel = intent.getIntExtra("level", -1);
                int rawLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                //int scale = intent.getIntExtra("scale", -1);
                int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
                int level = -1;
                if (rawLevel >= 0 && scale > 0) {
                    level = (rawLevel * 100) / scale;
                }
                txtBattery.setText("Battery3 Level Remaining  :" + level + "%");
            }

        };

        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            txtBattery = (TextView) findViewById(R.id.batterymeter_txt);
            batteryLevel();

        }

        private void batteryLevel() {
            IntentFilter batteryLevelFliter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
            registerReceiver(mBatteryLevelReciver, batteryLevelFliter);
        }
}
  • 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-17T12:14:21+00:00Added an answer on June 17, 2026 at 12:14 pm

    Move the receiver variable to be a class attribute so it will be in memory when the intent is received:

    public class Main extends Activity {
    
        private TextView txtBattery;
    
        private BroadcastReceiver mBatteryLevelReciver = new BroadcastReceiver() {
    
            @Override
            public void onReceive(Context context, Intent intent) {
    
                //int rawLevel = intent.getIntExtra("level", -1);
                int rawLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                //int scale = intent.getIntExtra("scale", -1);
                int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
                int level = -1;
                if (rawLevel >= 0 && scale > 0) {
                    level = (rawLevel * 100) / scale;
                }
                txtBattery.setText("Battery Level Remaining  :" + level + "%");
            }
    
        };
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            txtBattery = (TextView) findViewById(R.id.batterymeter_txt);
            batteryLevel();
    
        }
    
        private void batteryLevel() {
            IntentFilter batteryLevelFliter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
            registerReceiver(batteryLevelReciver, batteryLevelFliter);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using this code to show events. Every event is clickable, but I
I am using this code to show scrolling images but I am only getting
I'm using this code to show messages: Common.getHandler().post(new Runnable(){ public void run(){ Toast.makeText(Common.getContext(), Text...,
Im using this code to show/hide a div: <a href=# class=show_hide>Show/hide</a> <div class=slidingDiv> My
I'm using this jquery code to show and hide divs on my page. As
After using this code to get my link to show me 3 database entrys
I'm currently using this code (per the codex) to show children on parent pages,
I am using this code in my website to show Google search results for
I am using this code to parse this date. It must show new date
I'm using this code for my web assignment: http://csstechniques.blogspot.sg/2007/05/super-simple-css-bars.html But how do I show

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.