码迷,mamicode.com
首页 > 移动开发 > 详细

App Widgets

时间:2014-07-13 18:03:10      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:android   des   style   blog   http   color   

http://developer.android.com/guide/practices/ui_guidelines/widget_design.html#design

http://developer.android.com/guide/topics/appwidgets/index.html

http://developer.android.com/guide/topics/appwidgets/host.html

http://www.cnblogs.com/alwaysyouare/archive/2010/01/06/1640219.html    digitalclock Widget

Declaring an App Widget in the Manifest


First, declare the AppWidgetProvider class in yourapplication‘sAndroidManifest.xml file. For example:

<receiver android:name="ExampleAppWidgetProvider" >
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
    <meta-data android:name="android.appwidget.provider"
               android:resource="@xml/example_appwidget_info" />
</receiver>


Adding the AppWidgetProviderInfo Metadata


The AppWidgetProviderInfo defines the essential qualities of an App Widget, such as its minimum layout dimensions, its initiallayout resource,how often to update the App Widget, and (optionally) a configuration Activity tolaunch at create-time.Define the AppWidgetProviderInfo object in an XML resource using a single<appwidget-provider> element and save it in the project‘sres/xml/ folder.

For example:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="40dp"
    android:minHeight="40dp"
    android:updatePeriodMillis="86400000"
    android:previewImage="@drawable/preview"
    android:initialLayout="@layout/example_appwidget"
    android:configure="com.example.android.ExampleAppWidgetConfigure" 
    android:resizeMode="horizontal|vertical"
    android:widgetCategory="home_screen|keyguard"
    android:initialKeyguardLayout="@layout/example_keyguard">
</appwidget-provider>


The updatePeriodMillis attribute defines how often the AppWidget framework should request an update from theAppWidgetProvider by calling theonUpdate() callback method. The actual updateis not guaranteed to occur exactly on time with this value and we suggestupdating as infrequently as possible—perhaps no more than once an hour toconserve the battery. You might also allow the user to adjust the frequency in aconfiguration—some people might want a stock ticker to update every 15minutes, or maybe only four times a day.

Note: If the device is asleep when itis time for an update (as defined byupdatePeriodMillis), then the device willwake up in order to perform the update. If you don‘t update more than once per hour, thisprobably won‘t cause significant problems for the battery life.If, however, you needto update more frequently and/or you do not need to update while the device is asleep,then you can instead perform updates based on an alarm that will not wake the device. To doso, set an alarm with an Intent that your AppWidgetProvider receives, using theAlarmManager. Set the alarm type to eitherELAPSED_REALTIME orRTC, which will only deliver the alarm when the device is awake. Then setupdatePeriodMillis to zero ("0").


Creating the App Widget Layout


You must define an initial layout for your App Widget in XML and save it inthe project‘sres/layout/ directory. You can design your App Widget using theView objects listedbelow, but before you begin designing your App Widget, please read andunderstand theApp WidgetDesign Guidelines.


# of Cells
(Columns or Rows)
Available Size (dp)
(minWidth or minHeight)
1 40dp
2 110dp
3 180dp
4 250dp
n 70 × n ? 30


Using the AppWidgetProvider Class


The AppWidgetProvider class extendsBroadcastReceiver as a convenienceclass to handle the App Widget broadcasts. The AppWidgetProvider receives onlythe event broadcasts thatare relevant to the App Widget, such as when the App Widget is updated, deleted,enabled, and disabled.When these broadcast events occur, the AppWidgetProvider receives the followingmethod calls:

onUpdate()
This is called to update the App Widget at intervals defined by theupdatePeriodMillis attribute in the AppWidgetProviderInfo (see Adding the AppWidgetProviderInfo 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 aconfiguration Activity, this method is not called when the user adds theApp Widget, but is called for the subsequent updates. It is the responsibility of the configuration Activity to perform the first update when configuration isdone. (See Creating an App Widget ConfigurationActivity below.)






App Widgets,布布扣,bubuko.com

App Widgets

标签:android   des   style   blog   http   color   

原文地址:http://blog.csdn.net/llrraa2010/article/details/37738789

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!