今天我们要写一下Android Widget的开发,由于快点凌晨,我就不说的太具体了,同志们就模仿吧!首先看一下效果图:

下面是Demo的详细步骤:

一、新建一个Android工程命名为:WidgetDemo.

二、准备素材,一个是Widget的图标,一个是Widget的背景。存放目录如下图:

三、修改string.xml文件如下:

[c-sharp]view plaincopy
  1. <?xmlversion="1.0"encoding="utf-8"?><resources><stringname="hello">HelloWorld,WidetDemo!</string><stringname="app_name">DaysToWorldCup</string></resources>

四、建立Widget内容提供者文件,我们在res下建立xml文件夹,并且新建一个widget_provider.xml代码入下:

[xhtml]view plaincopy
  1. <?xmlversion="1.0"encoding="utf-8"?><appwidget-providerxmlns:android="http://schemas.android.com/apk/res/android"android:minWidth="50dip"android:minHeight="50dip"android:updatePeriodMillis="10000"android:initialLayout="@layout/main"/>

五、修改main.xml布局,代码如下:

[xhtml]view plaincopy
  1. <?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@drawable/wordcup"><TextViewandroid:id="@+id/wordcup"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"android:textSize="12px"android:textColor="#ff0000"/></LinearLayout>

六、修改WidgetDemo.java代码如下:

[java]view plaincopy
  1. packagecom.android.tutor;importjava.util.Calendar;importjava.util.Date;importjava.util.GregorianCalendar;importjava.util.Timer;importjava.util.TimerTask;importandroid.appwidget.AppWidgetManager;importandroid.appwidget.AppWidgetProvider;importandroid.content.ComponentName;importandroid.content.Context;importandroid.widget.RemoteViews;publicclassWidetDemoextendsAppWidgetProvider{/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonUpdate(Contextcontext,AppWidgetManagerappWidgetManager,int[]appWidgetIds){Timertimer=newTimer();timer.scheduleAtFixedRate(newMyTime(context,appWidgetManager),1,60000);super.onUpdate(context,appWidgetManager,appWidgetIds);}privateclassMyTimeextendsTimerTask{RemoteViewsremoteViews;AppWidgetManagerappWidgetManager;ComponentNamethisWidget;publicMyTime(Contextcontext,AppWidgetManagerappWidgetManager){this.appWidgetManager=appWidgetManager;remoteViews=newRemoteViews(context.getPackageName(),R.layout.main);thisWidget=newComponentName(context,WidetDemo.class);}publicvoidrun(){Datedate=newDate();Calendarcalendar=newGregorianCalendar(2010,06,11);longdays=(((calendar.getTimeInMillis()-date.getTime())/1000))/86400;remoteViews.setTextViewText(R.id.wordcup,"距离南非世界杯还有"+days+"天");appWidgetManager.updateAppWidget(thisWidget,remoteViews);}}}

七、修改配置文件AndroidManifest.xml,代码如下:

[xhtml]view plaincopy
  1. <?xmlversion="1.0"encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.android.tutor"android:versionCode="1"android:versionName="1.0"><applicationandroid:icon="@drawable/icon"android:label="@string/app_name"><receiverandroid:name=".WidetDemo"android:label="@string/app_name"><intent-filter><actionandroid:name="android.appwidget.action.APPWIDGET_UPDATE"/></intent-filter><meta-dataandroid:name="android.appwidget.provider"android:resource="@xml/widget_provider"/></receiver></application><uses-sdkandroid:minSdkVersion="7"/></manifest>

八、点击运行(Ctrl+F11),之,运行成功后,我们长时间点击桌面,会出现如下俩个,依次点击,就可以看到最上面的效果图:

更多相关文章

  1. android系统信息(内存、cpu、sd卡、电量、版本)获取
  2. 【Android】在Android上使用OrmLite数据库框架 之 使用表配置文
  3. Android(安卓)8.0 状态栏信号显示、信号定制
  4. 在Android设备上运行纯java代码
  5. Android(安卓)Alarm驱动源代码分析(Alarm.c)
  6. Qt on Android:让 Qt Widgets 和 Qt Quick 应用全屏显示
  7. android资源
  8. Android(安卓)通过WebView和js的交互
  9. Android上传图片到七牛云看这篇就够了

随机推荐

  1. 使用多个左连接查询 - 点列值不正确
  2. MySQL 5.6 MRR 的存储过程完美诠释
  3. Amoeba for MySQL---分布式数据库Proxy解
  4. [MySQL复制异常]Cannot execute statemen
  5. MySQL用户管理(5.7.20-winx64)
  6. oracle中decode函数 VS mysql中的if函数
  7. mysql官方文档之Range Optimization
  8. MySqli 中预处理类 stmt
  9. 如何使用另一个表中的多个列替换表中的一
  10. 哪个更好的大查询或多个小查询?