Android系统App Widgets英文资料外文翻译文献.docx
- 文档编号:9728925
- 上传时间:2023-02-06
- 格式:DOCX
- 页数:11
- 大小:37.49KB
Android系统App Widgets英文资料外文翻译文献.docx
《Android系统App Widgets英文资料外文翻译文献.docx》由会员分享,可在线阅读,更多相关《Android系统App Widgets英文资料外文翻译文献.docx(11页珍藏版)》请在冰豆网上搜索。
Android系统AppWidgets英文资料外文翻译文献
Android系统AppWidgets英文资料外文翻译文献
AppWidgets
AppWidgetsareminiatureapplicationviewsthatcanbeembeddedinotherapplications(suchastheHomescreen)andreceiveperiodicupdates.TheseviewsarereferredtoasWidgetsintheuserinterface,andyoucanpublishonewithanAppWidgetprovider.AnapplicationcomponentthatisabletoholdotherAppWidgetsiscalledanAppWidgethost.ThescreenshotbelowshowstheMusicAppWidget.
ThisdocumentdescribeshowtopublishanAppWidgetusinganAppWidgetprovider.
TheBasics
TocreateanAppWidget,youneedthefollowing:
AppWidgetProviderInfoobject
DescribesthemetadataforanAppWidget,suchastheAppWidget'slayout,updatefrequency,andtheAppWidgetProviderclass.ThisshouldbedefinedinXML.
AppWidgetProviderclassimplementation
DefinesthebasicmethodsthatallowyoutoprogrammaticallyinterfacewiththeAppWidget,basedonbroadcastevents.Throughit,youwillreceivebroadcastswhentheAppWidgetisupdated,enabled,disabledanddeleted.
Viewlayout
DefinestheinitiallayoutfortheAppWidget,definedinXML.
Additionally,youcanimplementanAppWidgetconfigurationActivity.ThisisanoptionalActivitythatlauncheswhentheuseraddsyourAppWidgetandallowshimorhertomodifyAppWidgetsettingsatcreate-time.
Thefollowingsectionsdescribehowtosetupeachofthesecomponents.
DeclaringanAppWidgetintheManifest
First,declaretheAppWidgetProviderclassinyourapplication'sAndroidManifest.xmlfile.Forexample:
name="ExampleAppWidgetProvider"> name="android.appwidget.action.APPWIDGET_UPDATE"/> name="android.appwidget.provider" android: resource="@xml/example_appwidget_info"/> The nameattribute,whichspecifiestheAppWidgetProviderusedbytheAppWidget. The nameattribute.ThisattributespecifiesthattheAppWidgetProvideracceptstheACTION_APPWIDGET_UPDATEbroadcast.Thisistheonlybroadcastthatyoumustexplicitlydeclare.TheAppWidgetManagerautomaticallysendsallotherAppWidgetbroadcaststotheAppWidgetProviderasnecessary. The •android: name-Specifiesthemetadataname.Useandroid.appwidget.providertoidentifythedataastheAppWidgetProviderInfodescriptor. •android: resource-SpecifiestheAppWidgetProviderInforesourcelocation. AddingtheAppWidgetProviderInfoMetadata TheAppWidgetProviderInfodefinestheessentialqualitiesofanAppWidget,suchasitsminimumlayoutdimensions,itsinitiallayoutresource,howoftentoupdatetheAppWidget,and(optionally)aconfigurationActivitytolaunchatcreate-time.DefinetheAppWidgetProviderInfoobjectinanXMLresourceusingasingle Forexample: android=" android: minWidth="294dp" --density-independentpixels--> android: minHeight="72dp" android: updatePeriodMillis="86400000" --onceperday--> android: initialLayout="@layout/example_appwidget" android: configure="com.example.android.ExampleAppWidgetConfigure"> Here'sasummaryofthe •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 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 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. Note: InAndroid1.5,thereisaknownissueinwhichtheonDeleted()methodwillnotbecalledwhenitshouldbe.Toworkaroundthisissue,youcanimplementonReceive()asdescribedinthisGroupposttoreceivetheonDeleted()callback. ThemostimportantAppWidgetProvidercallbackisonUpdated()becauseitiscalledwheneachAppWidgetisaddedtoahost(unlessyouuseaconfigurationActivity).IfyourAppWidgetacceptsanyuserinteraction
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Android系统App Widgets英文资料外文翻译文献 Android 系统 App Widgets 英文 资料 外文 翻译 文献
![提示](https://static.bdocx.com/images/bang_tan.gif)