Android 电池部件不更新
在创建电池部件失败后,我再次尝试使用不同的技术 浏览和研究之后,我决定在AppWidgetProvider类中使用服务和广播接收器 我的电池值始终为空,从不改变 我在清单中注册了该服务,并查看该服务是否已启动 我添加了日志,注意到在接收广播之前调用了Start命令 该值在onreceive中更改,但在更新视图的onstart命令中从未更改 我做错了什么 另外,我在哪里注销收件人 我的班级在下面Android 电池部件不更新,android,service,broadcastreceiver,power-management,Android,Service,Broadcastreceiver,Power Management,在创建电池部件失败后,我再次尝试使用不同的技术 浏览和研究之后,我决定在AppWidgetProvider类中使用服务和广播接收器 我的电池值始终为空,从不改变 我在清单中注册了该服务,并查看该服务是否已启动 我添加了日志,注意到在接收广播之前调用了Start命令 该值在onreceive中更改,但在更新视图的onstart命令中从未更改 我做错了什么 另外,我在哪里注销收件人 我的班级在下面 public class BatteryWidgetProvider extends AppWidge
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;
}
}
}
这是我的追踪
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
我做错了什么
您正试图从onStartCommand而不是onReceive更新应用程序小部件
我在哪里注销收件人
服务的onDestroy似乎是一个很可能做到这一点的地方。如果我在OnReceive中移动了所有代码,OnStart命令将用于什么?我太困惑了。。。。onReceived不允许我进行更新。此行在接收的组件名称thisWidget=new ComponentName this,BatteryWidgetProvider.class中不起作用@user1155204:如果我在OnReceive中移动了所有代码,OnStart命令将用于什么没什么,但我从来没有说过让你把所有的代码移到onReceive中。这一行行不通——这是因为BroadcastReceiver不是上下文。您需要使用提供给onReceive的上下文作为ComponentName构造函数的第一个参数。你帮了很多忙。