如何在启用时更新小部件布局?

时间:2013-01-13 09:00:57

标签: android

我正在为我的应用程序创建三个小部件,当我将三个小部件拖到主屏幕时,我得到相同的小部件布局,但在更新时小部件布局将会改变,

如何在拖动到主屏幕后立即更新三个小部件布局?

这是我的代码:

public class FullWidgetProvider extends AppWidgetProvider {


    private int widgetID;
    private static SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy  hh:mm:ss a");
    static String strWidgetText = "";



    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        // TODO Auto-generated method stub
        //super.onDeleted(context, appWidgetIds);
    //  Toast.makeText(context, "onDeleted()", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onDisabled(Context context) {
        // TODO Auto-generated method stub
        //super.onDisabled(context);
        //Toast.makeText(context, "onDisabled()", Toast.LENGTH_LONG).show();
    }



    @Override
    public void onEnabled(Context context) {
        // TODO Auto-generated method stub
        super.onEnabled(context);


    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onUpdate(context, appWidgetManager, appWidgetIds);

        final int N = appWidgetIds.length;
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];
            updateAppWidget(context, appWidgetManager, appWidgetId);

        }

    }

1 个答案:

答案 0 :(得分:2)

这是我的代码的一部分,但你可以得到一个想法

@Override 
public void onEnabled(Context context) {
    super.onEnabled(context);

    AppWidgetManager appManager = AppWidgetManager.getInstance(context); 
    ComponentName thisWidget = new ComponentName(context, ExampleAppWidgetProvider.class);
    RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget1);
    updateViews.setTextColor(R.id.widget1label, Color.YELLOW);
    appManager.updateAppWidget(thisWidget, updateViews); }