在创建电池小部件失败后,我再次尝试使用不同的技术。
在浏览和研究之后,我决定在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;
}
}
}
这是我的堆栈跟踪
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