第一步:新建一个led_clock.xml文件,用来写时钟的布局,我们用6个ImageView存放时钟的时、分、秒的数字图像,代码和效果如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/img0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"/>
<ImageView
android:id="@+id/img2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/img3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"/>
<ImageView
android:id="@+id/img4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/img5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
运行效果:
第二步:编写类LedClock,该类继承AppWidgrtManager, 代码如下:
public class LedClock extends AppWidgetProvider{
private Timer timer;
private AppWidgetManager appWidgetManager;
private Context context;
// 存放时钟的数字图像数组
private int digits[] =
{
R.drawable.time0,
R.drawable.time1,
R.drawable.time2,
R.drawable.time3,
R.drawable.time4,
R.drawable.time5,
R.drawable.time6,
R.drawable.time7,
R.drawable.time8,
R.drawable.time9
};
// 存放显示数字图像的ImageView数组
private int digitViews[] =
{
R.id.img0,
R.id.img1,
R.id.img2,
R.id.img3,
R.id.img4,
R.id.img5
};
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
// 创建一个定时器
timer = new Timer();
this.context = context;
this.appWidgetManager = appWidgetManager;
// 定时器每隔一秒发送一个空信息,用来更新控件上的消息
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(0x123);
}
},0,1000);
}
private Handler handler = new Handler(){
// 接收到空消息,进行以下操作
public void handleMessage(Message msg){
if(msg.what == 0x123){
// 创建RemoteViews对象,加载时钟的布局界面
RemoteViews remoteView = new RemoteViews(context.getPackageName(),R.layout.led_clock);
SimpleDateFormat sdf = new SimpleDateFormat("hhmmss");
String timeStr = sdf.format(new Date());
for(int i = 0;i < timeStr.length(); i++){
// 将数字字符减去48就能得到对应的数字
int num = timeStr.charAt(i) - 48;
// 对获取到的时钟布局界面的不同ImageView设置随时间变化的数字图像
remoteView.setImageViewResource(digitViews[i],digits[num]);
ComponentName componentName = new ComponentName(context,LedClock.class);
// 更新桌面控件
appWidgetManager.updateAppWidget(componentName,remoteView);
}
}
super.handleMessage(msg);
}
};
}
第三步:由于AppWidgetProvider继承了BroadcastReceiver,故在AndroidManifest.xml文件中配置receiver,代码如下:
<receiver android:name=".LedClock" android:label="ledClock">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="<span style="color:#ff0000;">@xml/my_clock</span>"/>
</receiver>
在res文件夹下新建xml文件夹,在该xml文件夹下建立my_clock.xml文件 , my_clock文件中定义的是对桌面控件的配置信息,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="150dip" // 桌面控件的最先宽度
android:minHeight="60dip" // 桌面控件的最小高度
android:updatePeriodMillis="5000" //桌面控件对系统时间的刷新频率
android:initialLayout="@layout/led_clock" // 桌面控件初始显示的布局
/>
第四步:启动程序后,在模拟器中找到WIDGET进入桌面控件列表,长按我们写好的桌面控件即可添加到桌面显示
注:使用SimpleDateFormat sdf = new SimpleDateFormat("hhmmss")是以十二小时制获得时间,想要获得二十四小时制的时间显示则将“hhmmss”换成“”HHmmss”