Android系统App Widgets英文资料外文翻译文献Word格式文档下载.docx
《Android系统App Widgets英文资料外文翻译文献Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《Android系统App Widgets英文资料外文翻译文献Word格式文档下载.docx(11页珍藏版)》请在冰豆网上搜索。
Viewlayout
DefinestheinitiallayoutfortheAppWidget,definedinXML.
Additionally,youcanimplementanAppWidgetconfigurationActivity.ThisisanoptionalActivitythatlauncheswhentheuseraddsyourAppWidgetandallowshimorhertomodifyAppWidgetsettingsatcreate-time.
Thefollowingsectionsdescribehowtosetupeachofthesecomponents.
DeclaringanAppWidgetintheManifest
First,declaretheAppWidgetProviderclassinyourapplication'
sAndroidManifest.xmlfile.Forexample:
<
receiverandroid:
name="
ExampleAppWidgetProvider"
>
<
intent-filter>
actionandroid:
android.appwidget.action.APPWIDGET_UPDATE"
/>
/intent-filter>
meta-dataandroid:
android.appwidget.provider"
android:
resource="
@xml/example_appwidget_info"
/receiver>
The<
receiver>
elementrequirestheandroid:
nameattribute,whichspecifiestheAppWidgetProviderusedbytheAppWidget.
elementmustincludean<
action>
elementwiththeandroid:
nameattribute.ThisattributespecifiesthattheAppWidgetProvideracceptstheACTION_APPWIDGET_UPDATEbroadcast.Thisistheonlybroadcastthatyoumustexplicitlydeclare.TheAppWidgetManagerautomaticallysendsallotherAppWidgetbroadcaststotheAppWidgetProviderasnecessary.
meta-data>
elementspecifiestheAppWidgetProviderInforesourceandrequiresthefollowingattributes:
•android:
name-Specifiesthemetadataname.Useandroid.appwidget.providertoidentifythedataastheAppWidgetProviderInfodescriptor.
resource-SpecifiestheAppWidgetProviderInforesourcelocation.
AddingtheAppWidgetProviderInfoMetadata
TheAppWidgetProviderInfodefinestheessentialqualitiesofanAppWidget,suchasitsminimumlayoutdimensions,itsinitiallayoutresource,howoftentoupdatetheAppWidget,and(optionally)aconfigurationActivitytolaunchatcreate-time.DefinetheAppWidgetProviderInfoobjectinanXMLresourceusingasingle<
appwidget-provider>
elementandsaveitintheproject'
sres/xml/folder.
Forexample:
appwidget-providerxmlns:
android="
minWidth="
294dp"
!
--density-independentpixels-->
minHeight="
72dp"
updatePeriodMillis="
86400000"
--onceperday-->
initialLayout="
@layout/example_appwidget"
configure="
com.example.android.ExampleAppWidgetConfigure"
/appwidget-provider>
Here'
sasummaryofthe<
attributes:
•ThevaluesfortheminWidthandminHeightattributesspecifytheminimumarearequiredbytheAppWidget'
slayout.
ThedefaultHomescreenpositionsAppWidgetsinitswindowbasedonagridofcellsthathaveadefinedheightandwidth.IfthevaluesforanAppWidget'
sminimumwidthorheightdon'
tmatchthedimensionsofthecells,thentheAppWidgetdimensionsrounduptothenearestcellsize.(SeetheAppWidgetDesignGuidelinesformoreinformationontheHomescreencellsizes.)
BecausetheHomescreen'
slayoutorientation(andthus,thecellsizes)canchange,asaruleofthumb,youshouldassumetheworst-casecellsizeof74pixelsfortheheightandwidthofacell.However,youmustsubtract2fromthefinaldimensiontoaccountforanyintegerroundingerrorsthatoccurinthepixelcount.Tofindyourminimumwidthandheightindensity-independentpixels(dp),usethisformula:
(numberofcells*74)–2
Followingthisformula,youshoulduse72dpforaheightofonecell,294dpandforawidthoffourcells.
•TheupdatePeriodMillisattributedefineshowoftentheAppWidgetframeworkshouldrequestanupdatefromtheAppWidgetProviderbycallingtheonUpdate()method.Theactualupdateisnotguaranteedtooccurexactlyontimewiththisvalueandwesuggestupdatingasinfrequentlyaspossible—perhapsnomorethanonceanhourtoconservethebattery.Youmightalsoallowtheusertoadjustthefrequencyinaconfiguration—somepeoplemightwantastocktickertoupdateevery15minutes,ormaybeonlyfourtimesaday.
Note:
Ifthedeviceisasleepwhenitistimeforanupdate(asdefinedbyupdatePeriodMillis),thenthedevicewillwakeupinordertoperformtheupdate.Ifyoudon'
tupdatemorethanonceperhour,thisprobablywon'
tcausesignificantproblemsforthebatterylife.If,however,youneedtoupdatemorefrequentlyand/oryoudonotneedtoupdatewhilethedeviceisasleep,thenyoucaninsteadperformupdatesbasedonanalarmthatwillnotwakethedevice.Todoso,setanalarmwithanIntentthatyourAppWidgetProviderreceives,usingtheAlarmManager.SetthealarmtypetoeitherELAPSED_REALTIMEorRTC,whichwillonlydeliverthealarmwhenthedeviceisawake.ThensetupdatePeriodMillistozero("
0"
).
•TheinitialLayoutattributepointstothelayoutresourcethatdefinestheAppWidgetlayout.
•TheconfigureattributedefinestheActivitytolaunchwhentheuseraddstheAppWidget,inorderforhimorhertoconfigureAppWidgetproperties.Thisisoptional(readCreatinganAppWidgetConfigurationActivitybelow).
SeetheAppWidgetProviderInfoclassformoreinformationontheattributesacceptedbythe<
element.
CreatingtheAppWidgetLayout
YoumustdefineaninitiallayoutforyourAppWidgetinXMLandsaveitintheproject'
sres/layout/directory.YoucandesignyourAppWidgetusingtheViewobjectslistedbelow,butbeforeyoubegindesigningyourAppWidget,pleasereadandunderstandtheAppWidgetDesignGuidelines.
CreatingtheAppWidgetlayoutissimpleifyou'
refamiliarwithDeclaringLayoutinXML.However,youmustbeawarethatAppWidgetlayoutsarebasedonRemoteViews,whichdonotsupporteverykindoflayoutorviewwidget.
ARemoteViewsobject(and,consequently,anAppWidget)cansupportthefollowinglayoutclasses:
•FrameLayout
•LinearLayout
•RelativeLayout
Andthefollowingwidgetclasses:
•AnalogClock
•Button
•Chronometer
•ImageButton
•ImageView
•ProgressBar
•TextView
Descendantsoftheseclassesarenotsupported.
UsingtheAppWidgetProviderClass
YoumustdeclareyourAppWidgetProviderclassimplementationasabroadcastreceiverusingthe<
elementintheAndroidManifest(seeDeclaringanAppWidgetintheManifestabove).
TheAppWidgetProviderclassextendsBroadcastReceiverasaconvenienceclasstohandletheAppWidgetbroadcasts.TheAppWidgetProviderreceivesonlytheeventbroadcaststhatarerelevanttotheAppWidget,suchaswhentheAppWidgetisupdated,deleted,enabled,anddisabled.Whenthesebroadcasteventsoccur,theAppWidgetProviderreceivesthefollowingmethodcalls:
onUpdate(Context,AppWidgetManager,int[])
ThisiscalledtoupdatetheAppWidgetatintervalsdefinedbytheupdatePeriodMillisattributeintheAppWidgetProviderInfo(seeAddingtheAppWidgetProviderInfoMetadataabove).ThismethodisalsocalledwhentheuseraddstheAppWidget,soitshouldperformtheessentialsetup,suchasdefineeventhandlersforViewsandstartatemporaryService,ifnecessary.However,ifyouhavedeclaredaconfigurationActivity,thismethodisnotcalledwhentheuseraddstheAppWidget,butiscalledforthesubsequentupdates.ItistheresponsibilityoftheconfigurationActivitytoperformthefirstupdatewhenconfigurationisdone.(SeeCreatinganAppWidgetConfigurationActivitybelow.)
onDeleted(Context,int[])
ThisiscalledeverytimeanAppWidgetisdeletedfromtheAppWidgethost.
onEnabled(Context)
ThisiscalledwhenaninstancetheAppWidgetiscreatedforthefirsttime.Forexample,iftheuseraddstwoinstancesofyourAppWidget,thisisonlycalledthefirsttime.IfyouneedtoopenanewdatabaseorperformothersetupthatonlyneedstooccuronceforallAppWidgetinstances,thenthisisagoodplacetodoit.
onDisabled(Context)
ThisiscalledwhenthelastinstanceofyourAppWidgetisdeletedfromtheAppWidgethost.ThisiswhereyoushouldcleanupanyworkdoneinonEnabled(Context),suchasdeleteatemporarydatabase.
onReceive(Context,Intent)
Thisiscalledforeverybroadcastandbeforeeachoftheabovecallbackmethods.Younormallydon'
tneedtoimplementthismethodbecausethedefaultAppWidgetProviderimplementationfiltersallAppWidgetbroadcastsandcallstheabovemethodsasappropriate.
InAndroid1.5,thereisaknownissueinwhichtheonDeleted()methodwillnotbecalledwhenitshouldbe.Toworkaroundthisissue,youcanimplementonReceive()asdescribedinthisGroupposttoreceivetheonDeleted()callback.
ThemostimportantAppWidgetProvidercallbackisonUpdated()becauseitiscalledwheneachAppWidgetisaddedtoahost(unlessyouuseaconfigurationActivity).IfyourAppWidgetacceptsanyuserinteraction