新聞中心

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

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

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

        本文翻譯自Android Developers Blog:Introducing home screen widgets and the AppWidget framework

        本文引用地址:http://www.104case.com/article/201610/305849.htm

        Android 1.5 SDK一個令人興奮的新特性是AppWidget framework,這個框架允許開發者開發widgets,這些widgets可以被用戶拖到用戶的桌面并且可以交互。widgets可以提供一個full-featured apps的預覽,例如可以顯示即將到來的日歷事件,或者一首后臺播放的歌曲的詳細信息。

        當widgets被拖到桌面上,他們被指定一個保留的空間來顯示應用提供的自定義內容。用戶可以通過這個widget來和你的應用交互,例如暫停或切換歌曲。如果你有一個后臺服務,你可以按照你自己的schedule更新你的widget,或者使用AppWidget framework提供的一個自動的更新機制。

        在更高層次上,每個widget就是一個BroadcastReceiver,他們用XML metadata來描述widget的細節。AppWidget framework通過broadcast intents和你的widget通信,例如當需要更新的時候。Widget更新使用RemoteViews被構建和發送。這個RemoteViews被包裝成一個layout和特定內容來顯示到桌面上。

        你可以非常容易的添加widgets到你的應用中,在這篇文章里我將給一個簡單的例子:寫一個widget來顯示Wiktionary “Word of the day.”你可以從這里獲取所有的源代碼,我將在這里解釋Appwidget相關的代碼。

        首先,你需要一個XML metadata描述這個widget,包括你想在桌面上保留的區域,一個你想展示的初始的layout,和你打算何時更新。Android桌面默認使用cell-based layout,因而它會rounds你請求的尺寸為最接近的cell的尺寸。這是有點疑惑,不過這里有個公式可以幫助你:

        Minimum size in dip = (Number of cells * 74dip) – 2dip

        在這個例子中,我想使我們的widget占用2 cells的寬度和1 cell的高度,這意味著我應該請求的最小尺寸為146dip * 72dip。我們將要每天更新一次我們的widget,大約是每86,400,000毫秒更新一次。以下是我們的widget的XML metadata:

        接下來,讓我們把XML metadata捆綁到AndroidManifest的BroadcasrReicever:

        最后,讓我們寫BroadcastReceiver的代碼來處理AppWidget的請求。為了幫助widgets管理所有

        broadcasr事件,有個helper class叫AppWidgetProvider,這里我們將使用這個類。其中需要注意的最重要的一件事是我們將調用一個后臺服務執行定期的更新。這是因BroadcastReceivers是一個Application Not Responding(ANR) timer,這意味著如果運行時間太長,可能需要提示用戶強制關閉我們的應用。制作一個web請求可能需要花費一些時間,因此我們使用服務來避免ANR timeouts.

        /**

        * Define a simple widget that shows the Wiktionary “Word of the day.” To build

        * an update we spawn a background {@link Service} to perform the API queries.

        */

        public class WordWidget extends AppWidgetProvider {

        @Override

        public void onUpdate(Context context, AppWidgetManager appWidgetManager,

        int[] appWidgetIds) {

        // To prevent any ANR timeouts, we perform the update in a service

        context.startService(new Intent(context, UpdateService.class));

        }

        public static class UpdateService extends Service {

        @Override

        public void onStart(Intent intent, int startId) {

        // Build the widget update for today

        RemoteViews updateViews = buildUpdate(this);

        // Push update for this widget to the home screen

        ComponentName thisWidget = new ComponentName(this, WordWidget.class);

        AppWidgetManager manager = AppWidgetManager.getInstance(this);

        manager.updateAppWidget(thisWidget, updateViews);

        }

        /**

        * Build a widget update to show the current Wiktionary

        * “Word of the day.” Will block until the online API returns.

        */

        public RemoteViews buildUpdate(Context context) {

        // Pick out month names from resources

        Resources res = context.getResources();

        String[] monthNames = res.getStringArray(R.array.month_names);

        // Find current month and day

        Time today = new Time();

        today.setToNow();

        // Build today’s page title, like “Wiktionary:Word of the day/March 21″

        String pageName = res.getString(R.string.template_wotd_title,

        monthNames[today.month], today.monthDay);

        RemoteViews updateViews = null;

        String pageContent = “”;

        try {

        // Try querying the Wiktionary API for today’s word

        SimpleWikiHelper.prepareUserAgent(context);

        pageContent = SimpleWikiHelper.getPageContent(pageName, false);

        } catch (ApiException e) {

        Log.e(”WordWidget”, “Couldn’t contact API”, e);

        } catch (ParseException e) {

        Log.e(”WordWidget”, “Couldn’t parse API response”, e);

        }

        // Use a regular expression to parse out the word and its definition

        Pattern pattern = Pattern.compile(SimpleWikiHelper.WORD_OF_DAY_REGEX);

        Matcher matcher = pattern.matcher(pageContent);

        if (matcher.find()) {

        // Build an update that holds the updated widget contents

        updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_word);

        String wordTitle = matcher.group(1);

        updateViews.setTextViewText(R.id.word_title, wordTitle);


        上一頁 1 2 下一頁

        關鍵詞:

        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 泰顺县| 沁阳市| 四子王旗| 时尚| 仪陇县| 方城县| 永靖县| 开化县| 利川市| 横山县| 临潭县| 阿勒泰市| 阿图什市| 于都县| 鸡西市| 定远县| 九台市| 新河县| 久治县| 班戈县| 鹰潭市| 塘沽区| 双流县| 鄂伦春自治旗| 东台市| 鹿泉市| 竹山县| 营口市| 黄平县| 集贤县| 汶川县| 英超| 枣庄市| 西和县| 长阳| 庄河市| 梅河口市| 洪湖市| 宁津县| 黄冈市| 黔西县|