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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:13:06+00:00 2026-06-10T18:13:06+00:00

I am a New B to Android. I have been Able to get the

  • 0

I am a New B to Android. I have been Able to get the Battery Status/Level with the Following Code:

private void BattStatus() {
        BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                context.unregisterReceiver(this);
                int rawlevel = intent.getIntExtra("level", -1);
                int scale = intent.getIntExtra("scale", -1);
                int level = -1;
                if (rawlevel >= 0 && scale > 0) {
                    level = (rawlevel * 100) / scale;
                }
                batteryLevel = level;
                BattStatus.setText("Battery Level : " + batteryLevel + "%");

            }
        };
        IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(batteryLevelReceiver, batteryLevelFilter);
    }

I would Like to Store the Battery Level In A text file (Using a thread). Code :

public final Runnable DBThread = new Runnable() {
        String AllInfo = batteryLevel+"%"+" , "+new SimpleDateFormat("HH:mm , dd.MM.yy ").format(new Date());
        public void run() {
            try {
                Log.d("DBThread","Battery :"+batteryLevel);
                Log.d("DBThread","Updating DB");
                myDbHelper.CreateAndWriteFile(sdDir+"/", AllInfo );
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        mHandler.postAtTime(this, SystemClock.uptimeMillis() + 2000);
        Log.d("DBThread","Updated DB");
        Log.d("DBThread",AllInfo);

    }

Unfortunately the Battery Status/Level returns 0% in the text file, when I test it using the Log function in the thread it returns the correct value.

Please could some one be so kind to tell me what I am doing wrong or what I am not doing, and maybe provide me with a code snippet as I am new to Development.And Sorry If My Post is incorrect First timer on Stack Overflow 🙂

Thank you very much!

  • 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-10T18:13:08+00:00Added an answer on June 10, 2026 at 6:13 pm

    This is not a Thread. This is a Runnable, which is a piece of code that is made to run in a Thread.

    In your case, it runs in the handler thread, most likely the UI Thread.

    You probably start your thread before receiving the battery status, hence writing the default value (0) to the file.

    You don’t need a thread for that. You can write to the file immediately after your receive the broadcast, in the onReceive method.

    Edit

    There are a few things that don’t work in your code. You unregister the broadcast receiver, hence you don’t receive the battery level after the first time.

    You write the batteryLevel value without knowing if it has indeed been modified

    You write every 2 seconds without knowing there has been a change.

    I would suggest that you don’t unregister the BR, so you receive all battery level change. Then, in the onReceive, you append to the file the date and new value.

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

Sidebar

Related Questions

I have been able to get my BroadcastReceiver running with this: <receiver android:name=.AlarmReceiver android:enabled=true
I am new to android and i have been analyzing the android source code
I am still pretty new to Android development, and I have not been able
I am very new to PhoneGap yet have not been able to get some
I have not been able to get my code to hit the onReceive method
I'm new to Android & Java programming but have been programming in .NET for
I am new to Android. I have tried an sample application for status bar
I'm in the process of developing an Android app. I have been able to
I am new to Android location/google maps. And I have been playing around with
With my code below, I have been able to save a cookie, but as

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.