在线看毛片网站电影-亚洲国产欧美日韩精品一区二区三区,国产欧美乱夫不卡无乱码,国产精品欧美久久久天天影视,精品一区二区三区视频在线观看,亚洲国产精品人成乱码天天看,日韩久久久一区,91精品国产91免费

<menu id="6qfwx"><li id="6qfwx"></li></menu>
    1. <menu id="6qfwx"><dl id="6qfwx"></dl></menu>

      <label id="6qfwx"><ol id="6qfwx"></ol></label><menu id="6qfwx"></menu><object id="6qfwx"><strike id="6qfwx"><noscript id="6qfwx"></noscript></strike></object>
        1. <center id="6qfwx"><dl id="6qfwx"></dl></center>

            新聞中心

            EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 介紹桌面widgets和AppWidget框架(譯)

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

            作者: 時(shí)間:2016-10-08 來源:網(wǎng)絡(luò) 收藏

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

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

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

            當(dāng)widgets被拖到桌面上,他們被指定一個(gè)保留的空間來顯示應(yīng)用提供的自定義內(nèi)容。用戶可以通過這個(gè)widget來和你的應(yīng)用交互,例如暫?;蚯袚Q歌曲。如果你有一個(gè)后臺(tái)服務(wù),你可以按照你自己的schedule更新你的widget,或者使用AppWidget framework提供的一個(gè)自動(dòng)的更新機(jī)制。

            在更高層次上,每個(gè)widget就是一個(gè)BroadcastReceiver,他們用XML metadata來描述widget的細(xì)節(jié)。AppWidget framework通過broadcast intents和你的widget通信,例如當(dāng)需要更新的時(shí)候。Widget更新使用RemoteViews被構(gòu)建和發(fā)送。這個(gè)RemoteViews被包裝成一個(gè)layout和特定內(nèi)容來顯示到桌面上。

            你可以非常容易的添加widgets到你的應(yīng)用中,在這篇文章里我將給一個(gè)簡(jiǎn)單的例子:寫一個(gè)widget來顯示W(wǎng)iktionary “Word of the day.”你可以從這里獲取所有的源代碼,我將在這里解釋Appwidget相關(guān)的代碼。

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

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

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

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

            最后,讓我們寫B(tài)roadcastReceiver的代碼來處理AppWidget的請(qǐng)求。為了幫助widgets管理所有

            broadcasr事件,有個(gè)helper class叫AppWidgetProvider,這里我們將使用這個(gè)類。其中需要注意的最重要的一件事是我們將調(diào)用一個(gè)后臺(tái)服務(wù)執(zhí)行定期的更新。這是因BroadcastReceivers是一個(gè)Application Not Responding(ANR) timer,這意味著如果運(yùn)行時(shí)間太長(zhǎng),可能需要提示用戶強(qiáng)制關(guān)閉我們的應(yīng)用。制作一個(gè)web請(qǐng)求可能需要花費(fèi)一些時(shí)間,因此我們使用服務(wù)來避免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 下一頁

            關(guān)鍵詞:

            評(píng)論


            相關(guān)推薦

            技術(shù)專區(qū)

            關(guān)閉