您的位置:首页 > 移动开发

Android中AppWidget加载流程(二)

2011-09-20 17:34 483 查看
1. 用户长按Launcher弹出添快捷组件的Dialog,选择添加AppWidget,这时会打开一个Activity(AppWidgetPickActivity)显示系统中全部

AppWidget,单击某一个AppWidget后会做以下操作:

(1) 通过AppWidgetService获取一个最新的appWidgetId值。

(2) 根据获取的appWidgetId值,向单击的AppWidget组件发出APPWIDGET_ENABLED和APPWIDGET_UPDATE更新广播。并向

/data/system/appwidgets.xml中添加此AppWidget的基本信息。

2. 启动AppWidgetPickActivity(第1步中提到)时使用的是startActivityForResult()方法,所以在向所单击的AppWidget组件发出

APPWIDGET_ENABLED和APPWIDGET_UPDATE更新广播后会执行onActivityResult()方法,做以下准备工作:

(1) 将需要被添加到Launcher的AppWidget基本信息保存到数据库中。

(2) 根据查找到的appWidgetId值(整型值)创建LauncherAppWidgetHostView布局对象。

(3) 根据查找到的appWidgetId值(整型值)从AppWidgetService中获取RemoteViews对象(因为是第一次启动所以RemoteViews对象

为空)。

(4) 将获取到的RemoteViews对象的布局解析并设置到第(2)步中创建的LauncherAppWidgetHostView布局对象中。

(5) 将LauncherAppWidgetHostView布局对象添加到Launcher的WorkSpace中(因为RemoteViews对象为空,所以只在Launcher的

WorkSpace中占了一个位置)。

3. 在第1步中已经向需要添加到Launcher中的AppWidget发出了更新消息(注意:第一步和第二步是异步执行的),继续做以下事情:

(1) 每个AppWidget接收到广播后都会调用onEnabled()方法和onUpdate()方法,在onEnabled()方法中进行一些初始化操作,在

onUpdate()方法中创建RemoteViews布局对象并通过AppWidgetManager的updateAppWidget(int appWidgetId, RemoteViews

remoteViews)方法通知AppWidgetService对象用RemoteViews对象更新appWidgetId所对应的AppWidget.

(2) AppWidgetService接收到了appWidgetId和RemoteViews后,通过appWidgetId查找已经被添加到Launcher的

LauncherAppWidgetHostView布局对象,并将RemoteViews中的布局更新到LauncherAppWidgetHostView布局对象中。

AppWidget显示在Launcher中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: