AppWidgetHost类、AppWidgetHostView类

http://blog.sina.com.cn/s/blog_5da93c8f0100ydtp.html

AppWidgetHost
AppWidgetHost是真正容纳AppWidget的地方,它的主要功能有两个:
(1) 监听来自AppWidgetService的事件,这是主要处理update和provider_changed两个事件,根据这两个事件更新widget。
class UpdateHandler extends Handler {
public UpdateHandler(Looper looper) {
super(looper);
}

public void handleMessage(Message msg) {
switch (msg.what) {
case HANDLE_UPDATE: {//更新事件
updateAppWidgetView(msg.arg1, (RemoteViews)msg.obj);
break;
}
case HANDLE_PROVIDER_CHANGED: {//升级事件
onProviderChanged(msg.arg1, (AppWidgetProviderInfo)msg.obj);
break;
}
}
}
}
(2)另外一个功能就是创建AppWidgetHostView。前面我们说过RemoteViews不是真正的View,只是View的描述,而AppWidgetHostView才是真正的View。这里先创建AppWidgetHostView,然后通过AppWidgetService查询appWidgetId对应的RemoteViews,最后把RemoteViews传递给AppWidgetHostView去updateAppWidget。

protected AppWidgetHostView onCreateView(Context context, int appWidgetId,
AppWidgetProviderInfo appWidget) {
return new AppWidgetHostView(context);
}

更新和升级AppWidget实现:通过appWidgetId,获得相应的AppWidgetHostView 。
protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget) {
AppWidgetHostView v;

你可能感兴趣的:(Android)