android手把手教你开发launcher(四)——显示widget

来源:互联网 发布:2017美国名校录取数据 编辑:程序博客网 时间:2024/05/16 05:01

由于本人的项目目前暂时不添加小部件,所以我就不做跟着本文做了,以下是原内容。原文地址: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中,详情看代码注释
[java] view plaincopyprint?
  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 plaincopyprint?
  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.    }  

android手把手教你开发launcher(一)(AndroidStudio版)

android手把手教你开发launcher(二)——列出安装的应用程序

android手把手教你开发launcher(三)——启动安装的应用程序

android手把手教你开发launcher(四)——显示widget

android手把手教你开发launcher(五)——设置壁纸



转自:http://www.bangchui.org/read.php?tid=12239

0 0
原创粉丝点击