android小部件不会更新

时间:2013-10-21 16:53:01

标签: android widget

我正在为Android开发一个数字小部件,我不得不更新我的小部件

我设置了一个更新覆盖并使用Bitmaps显示带字体的小时和分钟但是当我将其添加到我手机的主屏幕时它没有更新我的意思是它没有显示更新的时间

这是我的widget类: 首先我有更新覆盖:

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {

    super.onUpdate(context, appWidgetManager, appWidgetIds);

    for (int i = 0; i < appWidgetIds.length; i++) {

        int appWidgetId = appWidgetIds[i];

        Time today = new Time(Time.getCurrentTimezone());

        today.setToNow();

        int hour = today.hour;

        CharSequence h = Integer.toString(hour);

        String hh = h.toString();

        int minute = today.minute;

        CharSequence m = Integer.toString(minute);

        String mm = ":" + m.toString();

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

        views.setImageViewBitmap(R.id.imageView1, getFontBitmap(context, hh, Color.WHITE, 70));

        views.setImageViewBitmap(R.id.imageView2, getFontBitmap1(context, mm, Color.WHITE, 70));

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

然后是Bitmaps

public static Bitmap getFontBitmap(Context context, String text, int color, float fontSizeSP) {
    int fontSizePX = convertDiptoPix(context, fontSizeSP);
    int pad = (fontSizePX / 9);
    Paint paint = new Paint();
    Typeface typeface = Typeface.createFromAsset(context.getAssets(), "bold.ttf");
    paint.setAntiAlias(true);
    paint.setTypeface(typeface);
    paint.setColor(color);
    paint.setTextSize(fontSizePX);

    int textWidth = (int) (paint.measureText(text) + pad * 2);
    int height = (int) (fontSizePX / 0.75);
    Bitmap bitmap = Bitmap.createBitmap(textWidth, height, Bitmap.Config.ARGB_4444);
    Canvas canvas = new Canvas(bitmap);
    float xOriginal = pad;
    canvas.drawText(text, xOriginal, fontSizePX, paint);
    return bitmap;
}
public static Bitmap getFontBitmap1(Context context, String text, int color, float fontSizeSP) {
    int fontSizePX = convertDiptoPix(context, fontSizeSP);
    int pad = (fontSizePX / 9);
    Paint paint = new Paint();
    Typeface typeface = Typeface.createFromAsset(context.getAssets(), "thin.ttf");
    paint.setAntiAlias(true);
    paint.setTypeface(typeface);
    paint.setColor(color);
    paint.setTextSize(fontSizePX);

    int textWidth = (int) (paint.measureText(text) + pad * 2);
    int height = (int) (fontSizePX / 0.75);
    Bitmap bitmap = Bitmap.createBitmap(textWidth, height, Bitmap.Config.ARGB_4444);
    Canvas canvas = new Canvas(bitmap);
    float xOriginal = pad;
    canvas.drawText(text, xOriginal, fontSizePX, paint);
    return bitmap;
}
public static int convertDiptoPix(Context context, float dip) {
    int value = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, context.getResources().getDisplayMetrics());
    return value;
}

这是我在res / xml中的widget.xml:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
    android:minWidth="288dp" 
    android:minHeight="144dp" 
    android:updatePeriodMillis="10000" 
    android:initialLayout="@layout/widget_layout">
</appwidget-provider>

这是我的接收器

           <receiver android:name="widget">
       <intent-filter>
           <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
       </intent-filter>
       <meta-data android:name="android.appwidget.provider"
           android:resource="@xml/widget"/>
   </receiver>

所以有什么帮助?

0 个答案:

没有答案