Android Widget 桌面数字时钟(DigtalClockWidget)实例(3)

step3:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="cn.roco.widget.clock" android:versionCode="1"
 android:versionName="1.0">
 <uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
  <!-- 指定广播接受者 -->
  <receiver android:name="TimeWidgetProvider">
   <intent-filter>
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
   </intent-filter>
   <meta-data android:name="android.appwidget.provider"
    android:resource="@xml/appwidget_provider" /><!-- 指定provider -->
  </receiver>
  <!-- 指定时钟服务 -->
  <service android:name=".TimeService" />
 </application>
</manifest>

step4:TimeService.java

package cn.roco.widget.clock;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;
import android.widget.RemoteViews;

public class TimeService extends Service {
 // 将0~9的液晶数字图片定义成数组
 private int[] digits = new int[] { R.drawable.digit00, R.drawable.digit01,
   R.drawable.digit02, R.drawable.digit03, R.drawable.digit04,
   R.drawable.digit05, R.drawable.digit06, R.drawable.digit07,
   R.drawable.digit08, R.drawable.digit09, };

// 将显示年 月 日 小时、分钟、秒钟的ImageView定义成数组
 private int[] digitViews = new int[] { R.id.img01, R.id.img02, R.id.img03,
   R.id.img04, R.id.img06, R.id.img07, R.id.img09, R.id.img10,
   R.id.img12, R.id.img13, R.id.img15, R.id.img16, R.id.img18,
   R.id.img19 };

private Timer timer;

private TimerTask timerTask = new TimerTask() {
  @Override
  public void run() {

/** 获取并格式化当前时间 */
   SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
   String time = sdf.format(new Date());
   /** 获得显示时间的View */
   RemoteViews views = new RemoteViews(getPackageName(), R.layout.main);
   for (int i = 0; i < time.length(); i++) {
    // 将第i个数字字符转换为对应的数字
    int num = time.charAt(i) - 48;
    // 将第i张图片设为对应的液晶数字图片
    views.setImageViewResource(digitViews[i], digits[num]);
   }
   /** 调用拨号程序 */
   Intent intent = new Intent(Intent.ACTION_VIEW,
     Uri.parse("tel:10086"));
   PendingIntent pendingIntent = PendingIntent.getActivity(
     getApplicationContext(), 0, intent, 0);
   views.setOnClickPendingIntent(R.id.digit, pendingIntent);

/** 更新时间的显示 */
   AppWidgetManager appWidgetManager = AppWidgetManager
     .getInstance(getApplicationContext());
   // 将AppWidgetProvider子类实例包装成ComponentName对象
   ComponentName componentName = new ComponentName(
     getApplicationContext(), TimeWidgetProvider.class);
   // 调用AppWidgetManager将remoteViews添加到ComponentName中
   appWidgetManager.updateAppWidget(componentName, views);
  }
 };

@Override
 public void onCreate() {
  super.onCreate();
  // 定义计时器
  timer = new Timer();
  // 启动周期性调度
  timer.schedule(timerTask, 0, 1000);
 }

@Override
 public IBinder onBind(Intent intent) {
  return null;
 }

@Override
 public void onDestroy() {
  super.onDestroy();
  timer.cancel();
  timer = null;
 }

}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/7cd0c6306c91cc6d373f9661e015ec65.html