电池小工具不会更新

时间:2012-04-21 19:13:36

标签: android service broadcastreceiver power-management

在创建电池小部件失败后,我再次尝试使用其他技术。

在浏览和研究之后,我决定在我的AppWidgetProvider课程中使用服务和广播接收器。

我的电池值始终为空且永不改变。

我在清单中注册了该服务,并看到该服务已启动。

我添加了日志并注意到在广播的onRecieve之前调用了Start命令。

onReceieve中的值会发生变化,但我在更新视图的onStartCommand中永远不会更改。

我做错了什么?

另外,我在哪里取消注册收件人?

我的课程在下面

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;
        }

    }
}

这是我的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 个答案:

答案 0 :(得分:0)

  

我做错了什么?

您正在尝试从onStartCommand()而不是onReceive()更新您的应用小部件。

  

我在哪里取消注册收件人?

onDestroy()的{​​{1}}似乎很可能会这样做。