【问题标题】:Updating AppWidget更新 AppWidget
【发布时间】:2018-07-17 04:31:41
【问题描述】:

我在 AppWidget 的按钮点击上启动了一个活动。用户在活动中输入一些数据,当用户关闭活动时,我想更新用户在 AppWidget 上的 TextView 中输入的数据。知道怎么做吗? 我已经从 AppWidget 成功启动了活动,唯一的问题是更新 AppWidget。

【问题讨论】:

    标签: android android-appwidget


    【解决方案1】:

    您可以使用RemoteViewsAppWidgetManager

    AppWidgetManager manager = AppWidgetManager.getInstance(context);
    
    RemoteViews remoteViews = new RemoteViews(
        context.getPackageName(), R.layout.widget);
    remoteViews.setTextViewText(R.id.textBoxId, "new textbox content");
    
    manager.updateAppWidget(appWidgetId, remoteViews);
    

    【讨论】:

    • 我应该在扩展 AppWidgetProvider 的类的哪个方法中添加这个?
    • sn-p 将在您的小部件中更新TextView。您写道,您希望在用户关闭 Activity 时更新您的小部件,以便 ActivityonPause 方法是一个好地方。
    • @AmitChintawar appWidgetId 来自哪里?
    【解决方案2】:

    您可以从宿主应用程序活动或片段中调用 appwidget 更新方法。

    这就是我的做法:

        int[] ids = AppWidgetManager.getInstance(getApplication()).
        getAppWidgetIds(new ComponentName(getApplication(), MyWidget.class));
        MyWidget myWidget = new MyWidget();
        myWidget.onUpdate(this, AppWidgetManager.getInstance(this),ids);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-07
      • 2013-08-09
      • 2011-10-20
      • 2015-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多