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

  • Home
  • SEARCH
  • 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 7602299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:22:37+00:00 2026-05-30T23:22:37+00:00

I have a battery widget, and for some reason it does not receive the

  • 0

I have a battery widget, and for some reason it does not receive the BATTERY_PLIGGED_AC integer when the phone is plugged into AC.

I have another widget for the BATTERY_PLUGGED_USB which works just fine.

I can’t see anything wrong with my code:

public void onReceive(Context context, Intent intent) {
    status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
    batterylevel = intent.getIntExtra("level", 0); 

    updateAppWidget(context);
}

public void updateAppWidget(Context context){
    RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.androidbatterywidget_layout);
    updateViews.setTextViewText(R.id.textView1, "      " + batterylevel + "%");

    if (status == BatteryManager.BATTERY_PLUGGED_USB)
        updateViews.setImageViewResource(R.id.imageView2, R.drawable.usb);
    else if (status == BatteryManager.BATTERY_PLUGGED_AC)
        updateViews.setImageViewResource(R.id.imageView2, R.drawable.bolt);
    else 
        updateViews.setImageViewResource(R.id.imageView2, R.drawable.empty);

Hopefully someone will be able to spot what I have done wrong. Thanks!

  • 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-30T23:22:38+00:00Added an answer on May 30, 2026 at 11:22 pm

    Figured a way around. Sorry for this post I should have looked at it for a little longer myself. For future reference if people seem to have the same problem, it seems to me that the USB value is bundled with the BatteryManager.BATTERY_STATUS_UNKNOWN, but not the AC. So instead all i did was assign a new private int charging, to the value of the BatteryManager.BATTERY_PLUGGED_AC variable. If this is true (and the device is charging throygh AC), then the value is 1, so I simply replaced my previous if statement with

    if (charging == 1)
            updateViews.setImageViewResource(R.id.imageView2, R.drawable.bolt); 
    

    Now the code reads;

     private int batterylevel = 0;
     private int status;
     private int charging;
       private BroadcastReceiver myReceiver = new BroadcastReceiver()
       {
    
       public void onReceive(Context context, Intent intent)
       {
    
         status = intent.getIntExtra("status", BatteryManager.BATTERY_HEALTH_UNKNOWN);
         charging = intent.getIntExtra("plugged", BatteryManager.BATTERY_PLUGGED_AC);
         batterylevel = intent.getIntExtra("level", 0); 
    
         updateAppWidget(context);
    
       }
    
    
       public void updateAppWidget(Context context){
        RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.androidbatterywidget_layout);
        updateViews.setTextViewText(R.id.textView1, "      " + batterylevel + "%");
    
        if (charging == 1)
            updateViews.setImageViewResource(R.id.imageView2, R.drawable.bolt); 
        else if (status == BatteryManager.BATTERY_PLUGGED_USB)
                updateViews.setImageViewResource(R.id.imageView2, R.drawable.usb);
        else 
            updateViews.setImageViewResource(R.id.imageView2, R.drawable.empty);
    

    Im sure that there is a far more “majestic” way to do this, but i was just puzzled at why the USB int was passed if it was plugged into USB (2), but not the AC (1).

    Thanks!

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

Sidebar

Related Questions

Have you noticed a significant battery drain when debugging Android application on the phone?
I have created a battery widget that uses a Service to listen for Battery
Windows Mobile devices have different behaviour for suspending when the device is on battery
I have a widget that periodically updates itself (hourly) to display top result of
Is it possible to have a battery notification in every application that is running?
I have a simple question. How do I get iPhone's battery level? [UIDevice currentDevice]
I have an app that nees to stay in background. It should check some
I want to have my control to make display remain turn on on some
My computer have lost the power of CMOS battery! And my computer is showing
I have a setting in my .emacs like this (display-battery-mode 1) . It displays

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.