新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 介紹桌面widgets和AppWidget框架(譯)

        介紹桌面widgets和AppWidget框架(譯)

        作者: 時間:2016-10-08 來源:網絡 收藏

        updateViews.setTextViewText(R.id.word_type, matcher.group(2));

        updateViews.setTextViewText(R.id.definition, matcher.group(3).trim());

        // When user clicks on widget, launch to Wiktionary definition page

        String definePage = res.getString(R.string.template_define_url,

        Uri.encode(wordTitle));

        Intent defineIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(definePage));

        PendingIntent pendingIntent = PendingIntent.getActivity(context,

        0 /* no requestCode */, defineIntent, 0 /* no flags */);

        updateViews.setOnClickPendingIntent(R.id.widget, pendingIntent);

        } else {

        // Didn’t find word of day, so show error message

        updateViews = new RemoteViews(context.getPackageName(),

        R.layout.widget_message);

        CharSequence errorMessage = context.getText(R.string.widget_error);

        updateViews.setTextViewText(R.id.message, errorMessage);

        }

        return updateViews;

        }

        @Override public IBinder onBind(Intent intent) {

        // We don’t need to bind to this service

        return null;

        }

        }

        }

        到這里,你已經完成了一個簡單的widget,它將顯示Wiktionary “Word of the day.”。當一個更新被請求時,我們讀在線API將最新的數據push到widget上。AppWidget framework會按我們的需要自動更新,例如當一個新的widget添加時,或者新的一天加載新的Word of the day.”。

        最后,這里給出一些建議。Widgets推薦被設計成longer-term的內容,不應該經常的被更新。超過每小時的頻繁更新會快速消耗掉電量和帶寬。建議盡可能的不要頻繁更新,或者讓你的用戶自定義一個更新周期。例如有些人可能想stock ticker每15分鐘更新一次,或者可能是一天更新四次。我將在我的另一篇文章giving at Google I/O討論節省電量的一些額外的策略。

        最后要提的一件比較酷的事是AppWidget framework對方向并不關心(is abstracted in both directions).這意味著你可以在兩個home screen都可以包含widgets。你的widgets可以被添加到任何一個支持AppWidgetframework的home screen上。

        我們已經開發了幾個自己的widgets,例如Calendar和Music widgets,但是我們更希望看到你開發的widgets.


        上一頁 1 2 下一頁

        關鍵詞:

        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 胶州市| 武冈市| 隆尧县| 江津市| 加查县| 靖西县| 长治市| 双牌县| 周至县| 新竹县| 丹阳市| 闽清县| 从化市| 桂平市| 基隆市| 称多县| 隆德县| 赤水市| 安顺市| 莱芜市| 桓台县| 通辽市| 乌拉特中旗| 镇江市| 阿图什市| 五寨县| 松潘县| 五河县| 新和县| 蚌埠市| 晋宁县| 沈阳市| 高唐县| 红河县| 报价| 敖汉旗| 辽中县| 济宁市| 广州市| 阳山县| 平凉市|