Sunday, 8 September 2013

Widget ImageView not updating

Widget ImageView not updating

I am trying to make a clock widget in which I have a ImageView with custom
font.
Here is the code by which I am updating the widget in 1 sec interval.
[code]
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);
}
}
[/code]
But my problem is the the time is updated only once. I can't find a solution.

No comments:

Post a Comment