android手把手教你開發launcher(四)——顯示widget

由於本人的項目目前暫時不添加小部件,所以我就不做跟着本文做了,以下是原內容。原文地址: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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章