1. 获取widget信息
获取widget其实非常简单,我们只需要发送一个请求到系统,系统就会打开widget的列表,然后我们选择一个即可。代码如下:
2. 添加widget的view到layout中
当选择一个widget后会通过onActivityResult 通知到activity,widget的信息被包含在 Intent data中,详情看代码注释
- void addWidget() {
- int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
- Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
- pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
- // start the pick activity
- startActivityForResult(pickIntent, [b]REQUEST_PICK_APPWIDGET[/b]);
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- // The pattern used here is that a user PICKs a specific application,
- // which, depending on the target, might need to CREATE the actual
- // target.
- // For example, the user would PICK_SHORTCUT for "Music playlist", and
- // we
- // launch over to the Music app to actually CREATE_SHORTCUT.
- if (resultCode == RESULT_OK) {
- switch (requestCode) {
- case REQUEST_PICK_APPWIDGET:
- addAppWidget(data);
- break;
- case REQUEST_CREATE_APPWIDGET:
- completeAddAppWidget(data);
- break;
- }
- }
- }
- void addAppWidget(Intent data) {
- // TODO: catch bad widget exception when sent
- int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
- -1);
- AppWidgetProviderInfo appWidget = mAppWidgetManager
- .getAppWidgetInfo(appWidgetId);
- //widget 包含设置信息不为空,则启动widget的设置界面
- if (appWidget.configure != null) {
- // Launch over to configure widget, if needed
- Intent intent = new Intent(
- AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
- intent.setComponent(appWidget.configure);
- intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
- startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
- } else {
- // widget 包含设置信息为空,直接添加widget到layout中
- // Otherwise just add it
- onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
- }
- }
- void startActivityForResultSafely(Intent intent, int requestCode) {
- try {
- startActivityForResult(intent, requestCode);
- } catch (ActivityNotFoundException e) {
- Toast.makeText(this, "activity_not_found", Toast.LENGTH_SHORT)
- .show();
- } catch (SecurityException e) {
- Toast.makeText(this, "activity_not_found", Toast.LENGTH_SHORT)
- .show();
- }
- }
- /**
- * 添加widget信息到layout中
- * @param data 包含了widget的信息
- */
- private void completeAddAppWidget(Intent data) {
- Bundle extras = data.getExtras();
- int appWidgetId = extras
- .getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
- Log.d(TAG, "dumping extras content=" + extras.toString());
- AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager
- .getAppWidgetInfo(appWidgetId);
- // Perform actual inflation because we're live
- synchronized (mLock) {
- //获取显示widget的view
- mHostView = mAppWidgetHost.createView(this, appWidgetId,
- appWidgetInfo);
- mHostView.setAppWidget(appWidgetId, appWidgetInfo);
- //将获取的view添加早layout中
- LayoutParams lp = new LinearLayout.LayoutParams(
- appWidgetInfo.minWidth, appWidgetInfo.minHeight);
- mainLayout.addView(mHostView, lp);
- mHostView.requestLayout();
- }
- }