www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當(dāng)前位置:首頁(yè) > 嵌入式 > 嵌入式軟件
[導(dǎo)讀] 本文翻譯自Android Developers Blog:Introducing home screen widgets and the AppWidget frameworkAndroid 1.5 SDK一個(gè)令人興奮的新特性是AppWidget framework,這個(gè)框架

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

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)用交互,例如暫停或切換歌曲。如果你有一個(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);[!--empirenews.page--]

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;

}

}

}

到這里,你已經(jīng)完成了一個(gè)簡(jiǎn)單的widget,它將顯示W(wǎng)iktionary “Word of the day.”。當(dāng)一個(gè)更新被請(qǐng)求時(shí),我們讀在線API將最新的數(shù)據(jù)push到widget上。AppWidget framework會(huì)按我們的需要自動(dòng)更新,例如當(dāng)一個(gè)新的widget添加時(shí),或者新的一天加載新的Word of the day.”。

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

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

我們已經(jīng)開發(fā)了幾個(gè)自己的widgets,例如Calendar和Music widgets,但是我們更希望看到你開發(fā)的widgets.

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

倫敦2024年8月29日 /美通社/ -- 英國(guó)汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認(rèn)證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時(shí)1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動(dòng) BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運(yùn)行,同時(shí)企業(yè)卻面臨越來越多業(yè)務(wù)中斷的風(fēng)險(xiǎn),如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報(bào)道,騰訊和網(wǎng)易近期正在縮減他們對(duì)日本游戲市場(chǎng)的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)開幕式在貴陽舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

要點(diǎn): 有效應(yīng)對(duì)環(huán)境變化,經(jīng)營(yíng)業(yè)績(jī)穩(wěn)中有升 落實(shí)提質(zhì)增效舉措,毛利潤(rùn)率延續(xù)升勢(shì) 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長(zhǎng) 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競(jìng)爭(zhēng)力 堅(jiān)持高質(zhì)量發(fā)展策略,塑強(qiáng)核心競(jìng)爭(zhēng)優(yōu)勢(shì)...

關(guān)鍵字: 通信 BSP 電信運(yùn)營(yíng)商 數(shù)字經(jīng)濟(jì)

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺(tái)與中國(guó)電影電視技術(shù)學(xué)會(huì)聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會(huì)上宣布正式成立。 活動(dòng)現(xiàn)場(chǎng) NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長(zhǎng)三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會(huì)上,軟通動(dòng)力信息技術(shù)(集團(tuán))股份有限公司(以下簡(jiǎn)稱"軟通動(dòng)力")與長(zhǎng)三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉