1

我正在尝试制作一个时钟小部件,其中我有一个带有自定义字体的 ImageView。

这是我以 1 秒为间隔更新小部件的代码。

[代码]

package com.coderzheaven.customfontinwidget;

import java.util.Calendar;
import java.util.TimerTask;

import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Typeface;
import android.os.Handler;
import android.widget.RemoteViews;

public class MyTimer extends TimerTask {
    RemoteViews remoteViews;
    static Context thisContext;
    AppWidgetManager appWidgetManager;
    ComponentName thisWidget;
    Handler mHandler;

    public MyTimer(Context context, AppWidgetManager appWidgetManager) {
        this.appWidgetManager = appWidgetManager;
        MyTimer.thisContext = context;

        remoteViews = new RemoteViews(thisContext.getPackageName(),
                R.layout.widget_main);

        thisWidget = new ComponentName(context, MyAppWidgetProvider.class);

        mHandler = new Handler();

        System.out.println("TIMER STARTED................");

    }

    @Override
    public synchronized void run() {

        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

        for (final int appWidgetId : allWidgetIds) {

            if (mHandler != null) {

                mHandler.post(new Runnable() {
                    public void run() {

                        System.out.println("UPDATING......" + getTodaysTime() + " ID = " + appWidgetId);

                        Bitmap bmp = buildUpdate(getTodaysTime());

                        setBitmap(remoteViews, R.id.imageView_txt, bmp);

                        appWidgetManager.updateAppWidget(appWidgetId,
                                remoteViews); 

                    }
                }

                );

            }

        }

    } 



    public static Bitmap buildUpdate(String time) {
        Bitmap myBitmap = Bitmap.createBitmap(250, 250, Bitmap.Config.ARGB_8888);
        Canvas myCanvas = new Canvas(myBitmap);
        Paint paint = new Paint();
        Typeface clock = Typeface.createFromAsset(thisContext.getAssets(),
                "digital-7.ttf");
        paint.setAntiAlias(true);
        paint.setSubpixelText(true);
        paint.setTypeface(clock);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.RED);
        paint.setTextSize(60);
        paint.setTextAlign(Align.CENTER);
        myCanvas.drawText(time, 120, 60, paint);
        return myBitmap;
    }

    private void setBitmap(RemoteViews views, int resId, Bitmap bitmap){
        Bitmap proxy = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(proxy);
        c.drawBitmap(bitmap, new Matrix(), null);
        views.setImageViewBitmap(resId, proxy);
    }

    public static String getTodaysTime() {
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        int seconds = c.get(Calendar.SECOND);
        return new StringBuilder().append(pad(hour)).append(":")
                .append(pad(minute)).append(":").append(pad(seconds)).toString();
    }

    private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }
}

[/代码]

但我的问题是时间只更新一次。我找不到解决方案。

这是我的 AppWidgetProviderClass [代码]

package com.coderzheaven.customfontinwidget;

import java.util.Timer;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;

public class MyAppWidgetProvider extends AppWidgetProvider {
    static Timer myTimer;
    Intent myIntent;
    MyTimer obj_MyTime;

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        System.out.println("onUpdate");
        startTimer(context);
    }

    void startTimer(Context context) {
        if (myTimer != null) {
            myTimer.cancel();
        }
        myTimer = new Timer();
        if (obj_MyTime == null)
            obj_MyTime = new MyTimer(context,
                    AppWidgetManager.getInstance(context));
        myTimer.scheduleAtFixedRate(obj_MyTime, 1, 1000);
    }

    /*
     * This is called when an instance the App Widget is created for the first
     * time.
     */
    @Override
    public void onEnabled(Context context) {
        startTimer(context);
        super.onEnabled(context);
    }

    /*
     * This is called for every broadcast and before each of the above callback
     * methods.
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
    }

    /* 
     * This is called When all instances of App Widget is deleted from the App
     * Widget host.
     */
    @Override 
    public void onDisabled(Context context) {
        System.out.println("All Widgets Deleted");
        // disposeWidget();
        super.onDisabled(context);
    }  

    /*
     * This is called every time an App Widget is deleted from the App Widget
     * host.
     */
    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
    }

    public void disposeWidget() {
        if (myTimer != null) {
            myTimer.cancel();
            // myTimer.notifyAll();
            myTimer = null;
        }
        if (obj_MyTime != null) {
            obj_MyTime.cancel();
            // obj_MyTime.notifyAll();
            obj_MyTime = null;
        }
    }
}

[/代码]

4

1 回答 1

0

我不知道我是否理解正确 - 尝试使用“TimerTask”代替“Handler”,它将在一段时间内更新(您将设置)例如:TimerTask

于 2013-09-08T13:03:17.823 回答