Android小部件布局更新(AnalogClock)

时间:2012-07-25 16:40:21

标签: android layout widget clock

我在更新AnalogClock资源和imageview时遇到了一些问题。我有我的班级

public class Widget extends AppWidgetProvider

public static class UpdateService extends Service

onStart将活动更改为设置

Context context = getApplicationContext();
try {
    Intent myIntent = new Intent(context, Info.class);
    myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(myIntent);
} catch(Exception e) {
    Log.e(LOG_TAG,"Error Settings Intents",e);
}

Info.class 中,我创建了色调选择器。保存设置后,我重新着色我的AnalogClock:

public void reColor(int color){
    // dial
    Drawable dial_bg = getResources().getDrawable(R.drawable.widgetdial);
    dial_bg.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
    // hour
    Drawable hour_bg = getResources().getDrawable(R.drawable.widgethour);
    hour_bg.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
    //minute
    Drawable min_bg = getResources().getDrawable(R.drawable.widgetminute);
    min_bg.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
    // clock
    findViewById(R.id.dial).setBackgroundDrawable(dial_bg);
    findViewById(R.id.AnalogClock).setBackgroundDrawable(dial_bg);
}

它适用于我当前的布局(AnalogClock已选择颜色)。所以我将颜色值保存到SharedPreferences并调用更新服务:

button_save.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Context context = getBaseContext();
        Intent forceUpIntent = new Intent(context, Widget.UpdateService.class);
        context.startService(forceUpIntent);
        finish();
    }
});

我开始buildUpdate:

public RemoteViews buildUpdate(Context context) {
    RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget);
    try {
        Log.d(LOG_TAG,"Updating Views: REDRAW");
        SharedPreferences settings = getSharedPreferences(COLOR_NAME, Context.MODE_PRIVATE);
        if (settings !=null) {
            int newColor = settings.getInt(RECOLOR, Color.argb(255, 255, 255, 255));
            if (widgetColor != newColor) {
                // dial
                Drawable dial_bg = getResources().getDrawable(R.drawable.widgetdial);
                dial_bg.setColorFilter(newColor, PorterDuff.Mode.MULTIPLY);
                // hour
                Drawable hour_bg = getResources().getDrawable(R.drawable.widgethour);
                hour_bg.setColorFilter(newColor, PorterDuff.Mode.MULTIPLY);
                //minute
                Drawable min_bg = getResources().getDrawable(R.drawable.widgetminute);
                min_bg.setColorFilter(newColor, PorterDuff.Mode.MULTIPLY);
                // clock
                //findViewById(R.id.dial).setBackgroundDrawable(dial_bg);
                //findViewById(R.id.AnalogClock).setBackgroundDrawable(dial_bg);
                updateViews.setImageViewBitmap(R.id.dial, drawableToBitmap(dial_bg));
                updateViews.setImageViewBitmap(R.id.AnalogClock, drawableToBitmap(dial_bg));
                widgetColor = newColor;
            }
        }

        Intent AlarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER).setComponent(new ComponentName("com.android.alarmclock", "com.android.alarmclock.AlarmClock"));
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, AlarmClockIntent, 0);
        updateViews.setOnClickPendingIntent(R.id.AnalogClock, pendingIntent);
    } catch(Exception e) {
        Log.e(LOG_TAG,"Error Updating Views",e);
    }
    return updateViews;
}

但它不起作用,警告:

07-25 16:26:45.615: WARN/AppWidgetHostView(103): updateAppWidget couldn't find any view, using error view
07-25 16:26:45.615: WARN/AppWidgetHostView(103): android.widget.RemoteViews$ActionException: view: android.widget.AnalogClock doesn't have method: setImageBitmap(android.graphics.Bitmap)

是否可以使用位图可绘制更新 AnalogClock 布局或 ImageView

0 个答案:

没有答案