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

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

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

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

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

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

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

首先,你需要一個XML metadata描述這個widget,包括你想在桌面上保留的區(qū)域,一個你想展示的初始的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:

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

broadcasr事件,有個helper class叫AppWidgetProvider,這里我們將使用這個類。其中需要注意的最重要的一件事是我們將調(diào)用一個后臺服務執(zhí)行定期的更新。這是因BroadcastReceivers是一個Application Not Responding(ANR) timer,這意味著如果運行時間太長,可能需要提示用戶強制關(guān)閉我們的應用。制作一個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);[!--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)完成了一個簡單的widget,它將顯示W(wǎng)iktionary “Word of the day.”。當一個更新被請求時,我們讀在線API將最新的數(shù)據(jù)push到widget上。AppWidget framework會按我們的需要自動更新,例如當一個新的widget添加時,或者新的一天加載新的Word of the day.”。

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

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

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

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

上海2022年11月10日 /美通社/ -- 全球化創(chuàng)新藥公司亞虹醫(yī)藥(股票代碼:688176.SH)今日宣布,其口服藥APL-1401治療中度至重度活動性潰瘍性結(jié)腸炎的新藥臨床研究申請(IND)獲美國食品藥品監(jiān)督管理局...

關(guān)鍵字: FDA GAN UPDATE

如果您已經(jīng)注冊了一個不錯的ENS名稱,并希望通過IPFS和以太坊名稱服務使您的站點/dapp可用。要使其成為部署腳本的無縫部分,需要執(zhí)行幾個步驟。 在我們開始之前。.. 在本指南

關(guān)鍵字: IP UPDATE CONTENT UI

4月26日消息,據(jù)微博大V@互聯(lián)網(wǎng)信徒王冠雄爆料,李國慶今日率4條大漢上門搶當當公章。 對此消息,新浪科技表示,李國慶確認已接管當當。 Update:新浪財經(jīng)快訊16:15顯示,當當網(wǎng)今日發(fā)布聲明,稱

關(guān)鍵字: 李國慶 UPDATE 互聯(lián)網(wǎng)

在 Windows 上運行 Linux ? 這其實不是新鮮事,20年前,就有黑客這么干過。 不過大體都是虛擬機的做法,速度慢,能跑的 Linux 應用也少。 三十年河東,三十年河西。微軟的開發(fā)者博客

關(guān)鍵字: Windows UPDATE LINUX內(nèi)核 微軟

1、基礎(chǔ)知識:1)系統(tǒng)調(diào)用是操作系統(tǒng)內(nèi)核和應用程序之間的接口,設(shè)備驅(qū)動程序是操作系統(tǒng)內(nèi)核和機器硬件之間的接口。設(shè)備驅(qū)動程序為應用程序屏蔽了硬件細節(jié),在應用程序看來

關(guān)鍵字: 編寫 WINCE驅(qū)動 CONTEXT MDASH

Context對象是如此常見和傳遞使用,它可能會很容易產(chǎn)生并不是你預期的情形。加載資源、啟動一個新的Activity、獲取系統(tǒng)服務、獲取內(nèi)部文件路徑以及創(chuàng)建view(其實還遠不止這

關(guān)鍵字: Android CONTEXT APPLICATION ACTIVITY

1、概念理解 術(shù)語 說明...

關(guān)鍵字: IP CONTEXT GE SCALE

摘要:針對沒有集成ISP功能的MCU系統(tǒng),提出了一種通用的嵌入式系統(tǒng)Flash在線編程(ISP)方案。該方案借用RAM作為ISP的程序存儲器,可降低系統(tǒng)成本并具有較大的靈活性。以MC68332平臺為例,詳細介紹了這種IS...

關(guān)鍵字: ISP 嵌入式系統(tǒng) UPDATE 存儲器

help-顯示命令的使用信息很多可執(zhí)行程序都支持--help選項,--help選項描述了命令支持的語法和選項。例如:[me@linuxbox ~]$ mkdir --helpUsage: mkdir [OPTION]...

關(guān)鍵字: 命令行 LINUX命令 CONTEXT AN

下周二,微軟4月補丁發(fā)布日(4月8日),微軟將推送Windows 8.1 Update,而那一天,也是Windows XP支持終止的日子。根據(jù)微軟發(fā)布的安全公告,屆時,XP將迎來最后一批補丁。微軟本月將發(fā)布4個補丁,其中...

關(guān)鍵字: Windows 微軟 代碼 UPDATE
關(guān)閉