赞
踩
前不久看了博客主 stonecao 关于AppWidget的两篇文章,在读本篇博客之前,请务必先读这两篇博客:
AppWidget实现机制分析–什么是桌面插件
AppWidget实现机制分析–launcher添加和删除appwidget深入分析
这篇文章是对这两篇文章的一个补充,主要是描述下当桌面挂件应用在更新之后,桌面挂件是怎么更新的
luancher注册监听
在launcher主界面(一个Activity)起来的时候,会在onCreate执行如下一段代码:
Launcher.java
//mAppWidgetHost是launcher承载AppWidgetView的宿主
mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
mAppWidgetHost.startListening();
AppWidgetHost.java : startListening
try {
//mCallbacks是比较重要的回调,它是在AppWidgetHost初始化的时候创建的
updatedIds = sService.startListening(mCallbacks,
mContextOpPackageName,mHostId,updatedViews);
}
catch (RemoteException e) {
throw new RuntimeException("system server dead?", e);
}
AppWidgetHost.java : Callbacks
static class Callbacks extends IAppWidgetHost.Stub {
private final WeakReference<Handler> mWeakHandler;
public Callbacks(Handler handler) {
mWeakHandler = new WeakReference<>(handler);
}
// 更新Widget
public void updateAppWidget(int appWidgetId, RemoteViews views) {
if (isLocalBinder() && views != null) {
views = views.clone();
}
Handler handler = mWeakHandler.get();
if (handler == null) {
return;
}
Message msg = handler.obtainMessage(HANDLE_UPDATE, appWidgetId, 0, views);
msg.sendToTarget();
}
// 当桌面挂件应用的privider发生变化的时候,会回调该方法
public void providerChanged(int appWidgetId, AppWidgetProviderInfo info) {
if (isLocalBinder() && info != null) {
info = info.clone();
}
Handler handler = mWeakHandler.get();
if (handler == null) {
return;
}
Message msg = handler.obtainMessage(HANDLE_PROVIDER_CHANGED,
appWidgetId, 0, info);
msg.sendToTarget();
}
public void providersChanged() {
Handler handler = mWeakHandler.get();
if (handler == null) {
return;
}
handler.obtainMessage(HANDLE_PROVIDERS_CHANGED).sendToTarget();
}
public void viewDataChanged(int appWidgetId, int viewId) {
Handler handler = mWeakHandler.get();
if (handler == null) {
return;
}
Message msg = handler.obtainMessage(HANDLE_VIEW_DATA_CHANGED,
appWidgetId, viewId);
msg.sendToTarget();
}
private static boolean isLocalBinder() {
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。