android手把手教你开发launcher(四)

第四课:显示widget

1. 获取widget信息

获取widget其实非常简单,我们只需要发送一个请求到系统,系统就会打开widget的列表,然后我们选择一个即可。代码如下:

?

2. 添加widget的view到layout中
当选择一个widget后会通过onActivityResult 通知到activity,widget的信息被包含在 Intent data中,详情看代码注释
[java]  view plain  copy
 print ?
  1. void addWidget() {  
  2.         int appWidgetId = mAppWidgetHost.allocateAppWidgetId();  
  3.         Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);  
  4.         pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);  
  5.         // start the pick activity  
  6.         startActivityForResult(pickIntent, [b]REQUEST_PICK_APPWIDGET[/b]);  
  7.     }  

[java]  view plain  copy
 print ?
  1. @Override  
  2.    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  3.        // The pattern used here is that a user PICKs a specific application,  
  4.        // which, depending on the target, might need to CREATE the actual  
  5.        // target.  
  6.   
  7.        // For example, the user would PICK_SHORTCUT for "Music playlist", and  
  8.        // we  
  9.        // launch over to the Music app to actually CREATE_SHORTCUT.  
  10.   
  11.        if (resultCode == RESULT_OK) {  
  12.            switch (requestCode) {  
  13.            case REQUEST_PICK_APPWIDGET:  
  14.                addAppWidget(data);  
  15.                break;  
  16.            case REQUEST_CREATE_APPWIDGET:  
  17.                completeAddAppWidget(data);  
  18.                break;  
  19.   
  20.            }  
  21.        }  
  22.    }  
  23.   
  24.    void addAppWidget(Intent data) {  
  25.        // TODO: catch bad widget exception when sent  
  26.        int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,  
  27.                -1);  
  28.        AppWidgetProviderInfo appWidget = mAppWidgetManager  
  29.                .getAppWidgetInfo(appWidgetId);  
  30.   
  31.        //widget 包含设置信息不为空,则启动widget的设置界面  
  32.        if (appWidget.configure != null) {  
  33.            // Launch over to configure widget, if needed  
  34.            Intent intent = new Intent(  
  35.                    AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);  
  36.            intent.setComponent(appWidget.configure);  
  37.            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);  
  38.   
  39.            startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);  
  40.        } else {  
  41.        //    widget 包含设置信息为空,直接添加widget到layout中  
  42.            // Otherwise just add it  
  43.            onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);  
  44.        }  
  45.    }  
  46.   
  47.    void startActivityForResultSafely(Intent intent, int requestCode) {  
  48.        try {  
  49.            startActivityForResult(intent, requestCode);  
  50.        } catch (ActivityNotFoundException e) {  
  51.            Toast.makeText(this"activity_not_found", Toast.LENGTH_SHORT)  
  52.                    .show();  
  53.        } catch (SecurityException e) {  
  54.            Toast.makeText(this"activity_not_found", Toast.LENGTH_SHORT)  
  55.                    .show();  
  56.        }  
  57.    }  
  58.   
  59.    /** 
  60.     * 添加widget信息到layout中  
  61.     * @param data 包含了widget的信息 
  62.     */  
  63.    private void completeAddAppWidget(Intent data) {  
  64.        Bundle extras = data.getExtras();  
  65.        int appWidgetId = extras  
  66.                .getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);  
  67.   
  68.        Log.d(TAG, "dumping extras content=" + extras.toString());  
  69.   
  70.        AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager  
  71.                .getAppWidgetInfo(appWidgetId);  
  72.   
  73.        // Perform actual inflation because we're live  
  74.        synchronized (mLock) {  
  75.               
  76.            //获取显示widget的view  
  77.            mHostView = mAppWidgetHost.createView(this, appWidgetId,  
  78.                    appWidgetInfo);  
  79.            mHostView.setAppWidget(appWidgetId, appWidgetInfo);  
  80.   
  81.            //将获取的view添加早layout中  
  82.            LayoutParams lp = new LinearLayout.LayoutParams(  
  83.                    appWidgetInfo.minWidth, appWidgetInfo.minHeight);  
  84.            mainLayout.addView(mHostView, lp);  
  85.   
  86.            mHostView.requestLayout();  
  87.        }  
  88.   
  89.    }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值