android手把手教你开发launcher(四)——显示widget
来源:互联网 发布:java直播平台源码 编辑:程序博客网 时间:2024/05/21 15:43
由于本人的项目目前暂时不添加小部件,所以我就不做跟着本文做了,以下是原内容。原文地址:http://www.bangchui.org/read.php?tid=12239
我们要达到这样的效果:点击“add widget” 后弹出widget列表,之后选择一个widget后显示在界面上,如下:
第四课:显示widget
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(); } }
android手把手教你开发launcher(一)(AndroidStudio版)
android手把手教你开发launcher(二)——列出安装的应用程序
android手把手教你开发launcher(三)——启动安装的应用程序
android手把手教你开发launcher(四)——显示widget
android手把手教你开发launcher(五)——设置壁纸
转自:http://www.bangchui.org/read.php?tid=12239
- android手把手教你开发launcher(四)——显示widget
- android手把手教你开发launcher(四)——显示widget
- android手把手教你开发launcher(四)——显示widget
- android手把手教你开发launcher(四)
- android手把手教你开发launcher——设置壁纸
- android手把手教你开发launcher(二)——列出安装的应用程序
- android手把手教你开发launcher(三)——启动安装的应用程序
- android手把手教你开发launcher(五)——设置壁纸
- android手把手教你开发launcher(二)——列出安装的应用程序
- android手把手教你开发launcher(三)——启动安装的应用程序
- android手把手教你开发launcher(五)——设置壁纸
- android手把手教你开发launcher(二)——列出安装的应用程序
- android手把手教你开发launcher(三)——启动安装的应用程序
- android手把手教你开发launcher(五)——设置壁纸
- android手把手教你开发launcher(一)
- android手把手教你开发launcher(二)
- android手把手教你开发launcher(三)
- android手把手教你开发launcher(五)
- UTF-8文件编码格式中有无签名问题汇总《转》
- Jquery Table 的基本操作
- 关于成员函数指针和普通函数指针的转换
- 2013.9.24 ArrayList和LinkedList的区别
- VS2010 Opened in another editor
- android手把手教你开发launcher(四)——显示widget
- boost epressive用法2
- 日期问题(华为2014年机试题——合肥——之三)
- i2c_client,i2c_adapter和I2C-core的简介
- 服务器、客户端之syslog()
- eclipse 开发的web项目
- 关于Android加载图片时的OOM的一些解决方法和优化
- 宝马740Li xDrive四驱 豪华商务车的小改款
- repo学习资料汇总