TextView小组件未更新

时间:2011-11-12 10:29:58

标签: android android-widget

我的TextView小工具未在主屏幕上更新。

文本的新值未显示在窗口小部件上,但旧值仍保持原样。这是我调用窗口小部件更新的窗口小部件类和活动的代码:

 public class MotivappWidget extends AppWidgetProvider {

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

        super.onUpdate(context, appWidgetManager, appWidgetIds);

        Intent intent = new Intent(context, ViewAll.class);
        PendingIntent pending = PendingIntent
                .getActivity(context, 0, intent, 0);

        RemoteViews updateViews = new RemoteViews(context.getPackageName(),
                R.layout.widget);

        // updateViews.setTextViewText(R.id.widget_text, input);
        updateViews.setOnClickPendingIntent(R.id.widget_text, pending);
        appWidgetManager.updateAppWidget(appWidgetIds, updateViews);

    }



 public class ViewAll extends Activity implements OnClickListener {
            @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.all_view);
        }

    protected void onResume() {
        super.onResume();
        updateWidgetText();
    }

        private void updateWidgetText() {
            LayoutInflater inflater = (LayoutInflater) getApplicationContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
            View vi_widgy = inflater.inflate(R.layout.widget, null); //there is a layout xml file called widget.xml already made
            TextView tv_widgy = (TextView) vi_widgy.findViewById(R.id.widget_text); //in widget.xml, there is a text view called widget_text
            tv_widgy.setText("Text has just changed."); //this text never appears on the widget home even after updating
            updateAllWidgets();
        }

        private void updateAllWidgets(){
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
            int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(this, MotivappWidget.class));
            if (appWidgetIds.length > 0) {
                new MotivappWidget().onUpdate(this, appWidgetManager, appWidgetIds);
            }
        }

    }

0 个答案:

没有答案