收藏 分享(赏)

Android毕业论文英文翻译.doc

上传人:HR专家 文档编号:11327143 上传时间:2020-03-17 格式:DOC 页数:11 大小:44.22KB
下载 相关 举报
Android毕业论文英文翻译.doc_第1页
第1页 / 共11页
Android毕业论文英文翻译.doc_第2页
第2页 / 共11页
Android毕业论文英文翻译.doc_第3页
第3页 / 共11页
Android毕业论文英文翻译.doc_第4页
第4页 / 共11页
Android毕业论文英文翻译.doc_第5页
第5页 / 共11页
点击查看更多>>
资源描述

1、App WidgetsApp Widgets are miniature application views that can be embedded in other applications (such as the Home screen) and receive periodic updates. These views are referred to as Widgets in the user interface, and you can publish one with an App Widget provider. An application component that i

2、s able to hold other App Widgets is called an App Widget host. The screenshot below shows the Music App Widget.This document describes how to publish an App Widget using an App Widget provider.The BasicsTo create an App Widget, you need the following:AppWidgetProviderInfo objectDescribes the metadat

3、a for an App Widget, such as the App Widgets layout, update frequency, and the AppWidgetProvider class. This should be defined in XML.AppWidgetProvider class implementation Defines the basic methods that allow you to programmatically interface with the App Widget, based on broadcast events. Through

4、it, you will receive broadcasts when the App Widget is updated, enabled, disabled and deleted.View layout Defines the initial layout for the App Widget, defined in XML.Additionally, you can implement an App Widget configuration Activity. This is an optional Activity that launches when the user adds

5、your App Widget and allows him or her to modify App Widget settings at create-time.The following sections describe how to setup each of these components.Declaring an App Widget in the ManifestFirst, declare the AppWidgetProvider class in your applications AndroidManifest.xml file. For example :The e

6、lement requires the android:name attribute, which specifies the AppWidgetProvider used by the App Widget.The element must include an element with the android:name attribute. This attribute specifies that the AppWidgetProvider accepts the ACTION_APPWIDGET_UPDATE broadcast. This is the only broadcast

7、that you must explicitly declare. The AppWidgetManager automatically sends all other App Widget broadcasts to the AppWidgetProvider as necessary.The element specifies the AppWidgetProviderInfo resource and requires the following attributes: android:name - Specifies the metadata name. Use android.app

8、widget.provider to identify the data as the AppWidgetProviderInfo descriptor. android:resource - Specifies the AppWidgetProviderInfo resource location.Adding the AppWidgetProviderInfo Metadata The AppWidgetProviderInfo defines the essential qualities of an App Widget, such as its minimum layout dime

9、nsions, its initial layout resource, how often to update the App Widget, and (optionally) a configuration Activity to launch at create-time. Define the AppWidgetProviderInfo object in an XML resource using a single element and save it in the projects res/xml/ folder.For example :android:minHeight=“7

10、2dp“android:updatePeriodMillis=“86400000“ android:initialLayout=“layout/example_appwidget“android:configure=“com.example.android.ExampleAppWidgetConfigure“ Heres a summary of the attributes: The values for the minWidth and minHeight attributes specify the minimum area required by the App Widgets lay

11、out.The default Home screen positions App Widgets in its window based on a grid of cells that have a defined height and width. If the values for an App Widgets minimum width or height dont match the dimensions of the cells, then the App Widget dimensions round up to the nearest cell size. (See the A

12、pp Widget Design Guidelines for more information on the Home screen cell sizes.)Because the Home screens layout orientation (and thus, the cell sizes) can change, as a rule of thumb, you should assume the worst-case cell size of 74 pixels for the height and width of a cell. However, you must subtrac

13、t 2 from the final dimension to account for any integer rounding errors that occur in the pixel count. To find your minimum width and height in density-independent pixels (dp), use this formula:(number of cells * 74) 2Following this formula, you should use 72 dp for a height of one cell, 294 dp and

14、for a width of four cells. The updatePeriodMillis attribute defines how often the App Widget framework should request an update from the AppWidgetProvider by calling the onUpdate() method. The actual update is not guaranteed to occur exactly on time with this value and we suggest updating as infrequ

15、ently as possibleperhaps no more than once an hour to conserve the battery. You might also allow the user to adjust the frequency in a configurationsome people might want a stock ticker to update every 15 minutes, or maybe only four times a day.Note: If the device is asleep when it is time for an up

16、date (as defined by updatePeriodMillis), then the device will wake up in order to perform the update. If you dont update more than once per hour, this probably wont cause significant problems for the battery life. If, however, you need to update more frequently and/or you do not need to update while

17、 the device is asleep, then you can instead perform updates based on an alarm that will not wake the device. To do so, set an alarm with an Intent that your AppWidgetProvider receives, using the AlarmManager. Set the alarm type to either ELAPSED_REALTIME or RTC, which will only deliver the alarm whe

18、n the device is awake. Then set updatePeriodMillis to zero (“0“). The initialLayout attribute points to the layout resource that defines the App Widget layout. The configure attribute defines the Activity to launch when the user adds the App Widget, in order for him or her to configure App Widget pr

19、operties. This is optional (read Creating an App Widget Configuration Activity below).See the AppWidgetProviderInfo class for more information on the attributes accepted by the element.Creating the App Widget Layout You must define an initial layout for your App Widget in XML and save it in the proj

20、ects res/layout/ directory. You can design your App Widget using the View objects listed below, but before you begin designing your App Widget, please read and understand the App Widget Design Guidelines.Creating the App Widget layout is simple if youre familiar with Declaring Layout in XML. However

21、, you must be aware that App Widget layouts are based on RemoteViews, which do not support every kind of layout or view widget.A RemoteViews object (and, consequently, an App Widget) can support the following layout classes: FrameLayout LinearLayout RelativeLayoutAnd the following widget classes : A

22、nalogClock Button Chronometer ImageButton ImageView ProgressBar TextViewDescendants of these classes are not supported.Using the AppWidgetProvider ClassYou must declare your AppWidgetProvider class implementation as a broadcast receiver using the element in the AndroidManifest (see Declaring an App

23、Widget in the Manifest above).The AppWidgetProvider class extends BroadcastReceiver as a convenience class to handle the App Widget broadcasts. The AppWidgetProvider receives only the event broadcasts that are relevant to the App Widget, such as when the App Widget is updated, deleted, enabled, and

24、disabled. When these broadcast events occur, the AppWidgetProvider receives the following method calls:onUpdate(Context, AppWidgetManager, int)This is called to update the App Widget at intervals defined by the updatePeriodMillis attribute in the AppWidgetProviderInfo (see Adding the AppWidgetProvid

25、erInfo Metadata above). This method is also called when the user adds the App Widget, so it should perform the essential setup, such as define event handlers for Views and start a temporary Service, if necessary. However, if you have declared a configuration Activity, this method is not called when

26、the user adds the App Widget, but is called for the subsequent updates. It is the responsibility of the configuration Activity to perform the first update when configuration is done. (See Creating an App Widget Configuration Activity below.)onDeleted(Context, int) This is called every time an App Wi

27、dget is deleted from the App Widget host. onEnabled(Context)This is called when an instance the App Widget is created for the first time. For example, if the user adds two instances of your App Widget, this is only called the first time. If you need to open a new database or perform other setup that

28、 only needs to occur once for all App Widget instances, then this is a good place to do it. onDisabled(Context) This is called when the last instance of your App Widget is deleted from the App Widget host. This is where you should clean up any work done in onEnabled(Context), such as delete a tempor

29、ary database. onReceive(Context, Intent) This is called for every broadcast and before each of the above callback methods. You normally dont need to implement this method because the default AppWidgetProvider implementation filters all App Widget broadcasts and calls the above methods as appropriate

30、.Note: In Android 1.5, there is a known issue in which the onDeleted() method will not be called when it should be. To work around this issue, you can implement onReceive() as described in this Group post to receive the onDeleted() callback.The most important AppWidgetProvider callback is onUpdated(

31、) because it is called when each App Widget is added to a host (unless you use a configuration Activity). If your App Widget accepts any user interaction events, then you need to register the event handlers in this callback. If your App Widget doesnt create temporary files or databases, or perform o

32、ther work that requires clean-up, then onUpdated() may be the only callback method you need to define. For example, if you want an App Widget with a button that launches an Activity when clicked, you could use the following implementation of AppWidgetProvider:public class ExampleAppWidgetProvider ex

33、tends AppWidgetProvider public void onUpdate(Context context, AppWidgetManager appWidgetManager, int appWidgetIds) final int N = appWidgetIds.length;/ Perform this loop procedure for each App Widget that belongs to this providerfor (int i=0; i Broadcast Receiver Lifecycle for more information). If y

34、our App Widget setup process can take several seconds (perhaps while performing web requests) and you require that your process continues, consider starting a Service in the onUpdated() method. From within the Service, you can perform your own updates to the App Widget without worrying about the App

35、WidgetProvider closing down due to an Application Not Responding (ANR) error. See the Wiktionary samples AppWidgetProvider for an example of an App Widget running a Service.Also see the ExampleAppWidgetProvider.java sample class.Receiving App Widget broadcast IntentsAppWidgetProvider is just a conve

36、nience class. If you would like to receive the App Widget broadcasts directly, you can implement your own BroadcastReceiver or override the onReceive(Context, Intent) callback. The four Intents you need to care about are : ACTION_APPWIDGET_UPDATE ACTION_APPWIDGET_DELETED ACTION_APPWIDGET_ENABLED ACT

37、ION_APPWIDGET_DISABLEDCreating an App Widget Configuration ActivityIf you would like the user to configure settings when he or she adds a new App Widget, you can create an App Widget configuration Activity. This Activity will be automatically launched by the App Widget host and allows the user to co

38、nfigure available settings for the App Widget at create-time, such as the App Widget color, size, update period or other functionality settings.The configuration Activity should be declared as a normal Activity in the Android manifest file. However, it will be launched by the App Widget host with th

39、e ACTION_APPWIDGET_CONFIGURE action, so the Activity needs to accept this Intent. For example:Also, the Activity must be declared in the AppWidgetProviderInfo XML file, with the android:configure attribute (see Adding the AppWidgetProvierInfo Metadata above). For example, the configuration Activity

40、can be declared like this:Notice that the Activity is declared with a fully-qualified namespace, because it will be referenced from outside your package scope.Thats all you need to get started with a configuration Activity. Now all you need is the actual Activity. There are, however, two important t

41、hings to remember when you implement the Activity: The App Widget host calls the configuration Activity and the configuration Activity should always return a result. The result should include the App Widget ID passed by the Intent that launched the Activity (saved in the Intent extras as EXTRA_APPWI

42、DGET_ID). The onUpdate() method will not be called when the App Widget is created (the system will not send the ACTION_APPWIDGET_UPDATE broadcast when a configuration Activity is launched). It is the responsibility of the configuration Activity to request an update from the AppWidgetManager when the

43、 App Widget is first created. However, onUpdate() will be called for subsequent updatesit is only skipped the first time.See the code snippets in the following section for an example of how to return a result from the configuration and update the App Widget.Updating the App Widget from the configura

44、tion ActivityWhen an App Widget uses a configuration Activity, it is the responsibility of the Activity to update the App Widget when configuration is complete. You can do so by requesting an update directly from the AppWidgetManager.Heres a summary of the procedure to properly update the App Widget

45、 and close the configuration Activity:1. First, get the App Widget ID from the Intent that launched the Activity: 2. Intent intent = getIntent();3. Bundle extras = intent.getExtras();4. if (extras != null) 5. mAppWidgetId = extras.getInt(6. AppWidgetManager.EXTRA_APPWIDGET_ID, 7. AppWidgetManager.IN

46、VALID_APPWIDGET_ID);8. 9. Perform your App Widget configuration. 10. When the configuration is complete, get an instance of the AppWidgetManager by calling getInstance(Context): 11. AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);12. Update the App Widget with a RemoteViews

47、 layout by calling updateAppWidget(int, RemoteViews): 13. RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);14. appWidgetManager.updateAppWidget(mAppWidgetId, views);15. Finally, create the return Intent, set it with the Activity result, and finish the Activit

48、y: 16. Intent resultValue = new Intent();17. resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);18. setResult(RESULT_OK, resultValue);19. finish();Tip: When your configuration Activity first opens, set the Activity result to RESULT_CANCELED. This way, if the user backs-out of the Activity before reaching the end, the App Widget host is notified that the configuration was cancelled and the App Widget will not be added.From: http:/www.linuxtopia.org/online_books/android/devguide/guide/topics/appwidgets/index.html

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 外语学习 > 英语学习

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报