Android中的AppWidget与google widget和中移动的widget并不是一个概念,这里的AppWidget只是把一个进程的控件嵌入到别外一个进程的窗口里的一种方法。View在另 外一个进程里显示,但事件的处理方法还是在原来的进程里。这有点像 X Window中的嵌入式窗口。
Android中的AppWidget包括以下几个部分:
AppWidgetProvider
AppWidgetProvider是AppWidget提供者需要实现的接口,它实际上是一个BroadcastReceiver。只不过子类要实现的不再是onReceive,而是转换成了几个新的函数:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) public void onDeleted(Context context, int[] appWidgetIds) public void onEnabled(Context context) public void onDisabled(Context context)
这几个函数用来响应AppWidgetService发出的相应的广播消息。
AppWidgetProvider的实现者
作为AppWidgetProvider的实现者,一定要实现onUpdate函数,因为这个函数决定widget的显示方式,如果没有这个函数widget根本没办法出现。
void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
onUpdate的实现基本上遵循下面的流程:
o 创建RemoteViews
o 调用AppWidgetManager的updateAppWidget去更新widget.
现在我们看下Music里的MediaAppWidgetProvider实现:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { defaultAppWidget(context, appWidgetIds); // Send broadcast intent to any running MediaPlaybackService so it can // wrap around with an immediate update. Intent updateIntent = new Intent(MediaPlaybackService.SERVICECMD); updateIntent.putExtra(MediaPlaybackService.CMDNAME, MediaAppWidgetProvider.CMDAPPWIDGETUPDATE); updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds); updateIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); context.sendBroadcast(updateIntent); }
在defaultAppWidget里面:
o 创建RemoteViews,并设置相应的属性。
final Resources res = context.getResources(); final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.album_appwidget); views.setViewVisibility(R.id.title, View.GONE); views.setTextViewText(R.id.artist, res.getText(R.string.emptyplaylist));
o 为View上的控制设置事件处理方法。
linkButtons(context, views, false /* not playing */); private void linkButtons(Context context, RemoteViews views, boolean playerActive) { // Connect up various buttons and touch events Intent intent; PendingIntent pendingIntent; final ComponentName serviceName = new ComponentName(context, MediaPlaybackService.class); if (playerActive) { intent = new Intent(context, MediaPlaybackActivity.class); pendingIntent = PendingIntent.getActivity(context, 0 /* no requestCode */, intent, 0 /* no flags */); views.setOnClickPendingIntent(R.id.album_appwidget, pendingIntent); } else { intent = new Intent(context, MusicBrowserActivity.class); pendingIntent = PendingIntent.getActivity(context, 0 /* no requestCode */, intent, 0 /* no flags */); views.setOnClickPendingIntent(R.id.album_appwidget, pendingIntent); } intent = new Intent(MediaPlaybackService.TOGGLEPAUSE_ACTION); intent.setComponent(serviceName); pendingIntent = PendingIntent.getService(context, 0 /* no requestCode */, intent, 0 /* no flags */); views.setOnClickPendingIntent(R.id.control_play, pendingIntent); intent = new Intent(MediaPlaybackService.NEXT_ACTION); intent.setComponent(serviceName); pendingIntent = PendingIntent.getService(context, 0 /* no requestCode */, intent, 0 /* no flags */); views.setOnClickPendingIntent(R.id.control_next, pendingIntent); }
o 更新widget
pushUpdate(service, appWidgetIds, views); private void pushUpdate(Context context, int[] appWidgetIds, RemoteViews views) { // Update specific list of appWidgetIds if given, otherwise default to all final AppWidgetManager gm = AppWidgetManager.getInstance(context); if (appWidgetIds != null) { gm.updateAppWidget(appWidgetIds, views); } else { gm.updateAppWidget(THIS_APPWIDGET, views); } }
RemoteViews
RemoteViews并不是一个真正的View,它没有实现View的接口,而只是一个用于描述View的实体。比如:创建View需要的资源ID和各个控件的事件响应方法。RemoteViews会通过进程间通信机制传递给AppWidgetHost。
现在我们可以看出,Android中的AppWidget与google widget和中移动的widget并不是一个概念,这里的AppWidget只是把一个进程的控件嵌入到别外一个进程的窗口里的一种方法。View在另 外一个进程里显示,但事件的处理方法还是在原来的进程里。这有点像 X Window中的嵌入式窗口。
AppWidgetHost
AppWidgetHost是真正容纳AppWidget的地方,它的主要功能有两个:
o 监听来自AppWidgetService的事件:
class Callbacks extends IAppWidgetHost.Stub { public void updateAppWidget(int appWidgetId, RemoteViews views) { Message msg = mHandler.obtainMessage(HANDLE_UPDATE); msg.arg1 = appWidgetId; msg.obj = views; msg.sendToTarget(); } public void providerChanged(int appWidgetId, AppWidgetProviderInfo info) { Message msg = mHandler.obtainMessage(HANDLE_PROVIDER_CHANGED); msg.arg1 = appWidgetId; msg.obj = info; msg.sendToTarget(); } }
这是主要处理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; } } } }
o 另外一个功能就是创建AppWidgetHostView。前面我们说过RemoteViews不是真正的View,只是View的描述,而 AppWidgetHostView才是真正的View。这里先创建AppWidgetHostView,然后通过AppWidgetService查询 appWidgetId对应的RemoteViews,最后把RemoteViews传递给AppWidgetHostView去 updateAppWidget。
public final AppWidgetHostView createView(Context context, int appWidgetId, AppWidgetProviderInfo appWidget) { AppWidgetHostView view = onCreateView(context, appWidgetId, appWidget); view.setAppWidget(appWidgetId, appWidget); synchronized (mViews) { mViews.put(appWidgetId, view); } RemoteViews views = null; try { views = sService.getAppWidgetViews(appWidgetId); } catch (RemoteException e) { throw new RuntimeException("system server dead?", e); } view.updateAppWidget(views); return view; }
AppWidgetHostView
AppWidgetHostView是真正的View,但它只是一个容器,用来容纳实际的AppWidget的View。这个AppWidget的View是根据RemoteViews的描述来创建。这是在updateAppWidget里做的:
public void updateAppWidget(RemoteViews remoteViews) { ... if (content == null && layoutId == mLayoutId) { try { remoteViews.reapply(mContext, mView); content = mView; recycled = true; if (LOGD) Log.d(TAG, "was able to recycled existing layout"); } catch (RuntimeException e) { exception = e; } } // Try normal RemoteView inflation if (content == null) { try { content = remoteViews.apply(mContext, this); if (LOGD) Log.d(TAG, "had to inflate new layout"); } catch (RuntimeException e) { exception = e; } } ... if (!recycled) { prepareView(content); addView(content); } if (mView != content) { removeView(mView); mView = content; } ... }
remoteViews.apply创建了实际的View,下面代码可以看出:
public View apply(Context context, ViewGroup parent) { View result = null; Context c = prepareContext(context); Resources r = c.getResources(); LayoutInflater inflater = (LayoutInflater) c .getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater = inflater.cloneInContext(c); inflater.setFilter(this); result = inflater.inflate(mLayoutId, parent, false); performApply(result); return result; }
Host的实现者
AppWidgetHost和AppWidgetHostView是在框架中定义的两个基类。应用程序可以利用这两个类来实现自己的Host。Launcher是缺省的桌面,它是一个Host的实现者。
LauncherAppWidgetHostView扩展了AppWidgetHostView,实现了对长按事件的处理。
LauncherAppWidgetHost扩展了AppWidgetHost,这里只是重载了onCreateView,创建LauncherAppWidgetHostView的实例。
AppWidgetService
AppWidgetService存在的目的主要是解开AppWidgetProvider和AppWidgetHost之间的耦合。如果 AppWidgetProvider和AppWidgetHost的关系固定死了,AppWidget就无法在任意进程里显示了。而有了 AppWidgetService,AppWidgetProvider根本不需要知道自己的AppWidget在哪里显示了。