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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:34:32+00:00 2026-06-02T10:34:32+00:00

After failing at creating a battery widget, I tried again using a different technique.

  • 0

After failing at creating a battery widget, I tried again using a different technique.

After browsing and researching, I decided to use a service and a broadcast receiever inside my AppWidgetProvider class.

My battery value is always null and never changes.

I registered the service in the manifest and see that the service is started.

I added logs and noticed the Start command is called before the onRecieve of the broadcast.

The value changes in the onReceieve but is never changed in the the onStartCommand where I update my view.

What am I doing wrong?

Also, where do I unregister the receiever?

My class is below

public class BatteryWidgetProvider extends AppWidgetProvider
{
    private RemoteViews views;
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) 
    {
        context.startService(new Intent(context, BatteryMonitorReceiver.class));
        /*context.getApplicationContext().registerReceiver(this, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        ComponentName name = new ComponentName(context, BatteryWidgetProvider.class);
        appWidgetManager.updateAppWidget(name, this.views);

        super.onUpdate(context, appWidgetManager, appWidgetIds);*/

    }

    public static class BatteryMonitorReceiver extends Service
    {
        private RemoteViews views;
        private Integer level;
        private BroadcastReceiver batteryReceiver = new BroadcastReceiver(){
            @Override
            public void onReceive( Context context, Intent intent )
            {
                String action = intent.getAction();
                if(action.equals(Intent.ACTION_BATTERY_CHANGED))
                {
                    level = intent.getIntExtra( "level", 0 );
                    Log.d("BATTERY ON Receieve", "Battery Level is "+level);
views = new RemoteViews(getPackageName(), R.layout.battery100layout);

                }
ComponentName thisWidget = new ComponentName(context,BatteryWidgetProvider.class);
                    AppWidgetManager manager = AppWidgetManager.getInstance(context);
                    manager.updateAppWidget(thisWidget, views);

            }
        };

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) 
        {
            registerReceiver( this.batteryReceiver, new IntentFilter( Intent.ACTION_BATTERY_CHANGED ) );

            Log.d("BATTERY Service", "Battery Level is "+level);


            super.onStart(intent, startId);
            return super.onStartCommand(intent, flags, startId);
        }

        @Override
        public void onStart(Intent intent, int startId) 
        {


        }

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

    }
}

Here is my stacktrace

java.lang.RuntimeException: Unable to start service com.mischel.batterywidget.BatteryWidgetProvider$BatteryMonitorReceiver@40522428 with null: java.lang.NullPointerException
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2473)
at android.app.ActivityThread.access$2800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1127)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:4385)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.test.batterywidget.BatteryWidgetProvider$BatteryMonitorReceiver.onStartCommand(BatteryWidgetProvider.java:102)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2456)
... 10 more
  • 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-02T10:34:34+00:00Added an answer on June 2, 2026 at 10:34 am

    What am I doing wrong?

    You are attempting to update your app widget from onStartCommand(), instead of from onReceive().

    Also where do I unregister the receiever?

    onDestroy() of the Service would seem like a likely spot to do that.

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

Sidebar

Related Questions

After failing to achieve this with link_to remote, I decided to try jQuery way.
Log4J is failing for us generally after a few hours of use in our
After failing to get the desired result with generics only, I decided to try
Ok, after failing to read a polynomial, I'm trying first a basic approach to
After trying to make a while(bool) loop and it failing because I couldn't see
I have a program failing with: terminate called after throwing an instance of 'std::bad_alloc'
I'm having a problem with doing a sendSynchronousRequest failing. It only fails after I
After some experience with functional languages, I'm starting to use recursion more in Java
After trying to format my JSON data by hand in javascript and failing miserably,
Out of nowhere today (possibly after a bundle update), all my Cukes started failing

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.