【问题标题】:how to call onUpdate() in AppWidgetProvider?如何在 AppWidgetProvider 中调用 onUpdate()?
【发布时间】:2011-06-22 08:57:45
【问题描述】:

这是我的代码...

String[] show = new String[2];
RemoteViews remoteViews;
public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";   

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

    remoteViews = new RemoteViews( context.getPackageName(), R.layout.widget );
    remoteViews.setTextViewText( R.id.widget_title, show[0]);
    remoteViews.setTextViewText( R.id.widget_textview, show[1]);

    Intent configIntent = new Intent(context, SuitAuto.class);
    configIntent.setAction(ACTION_WIDGET_CONFIGURE);

    Intent active = new Intent(context, WordWidget.class);
    active.setAction(ACTION_WIDGET_RECEIVER);
    active.putExtra("msg", "Message for Button 1");

    PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
    PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);

    remoteViews.setOnClickPendingIntent(R.id.update, actionPendingIntent);
    remoteViews.setOnClickPendingIntent(R.id.Lilo, configPendingIntent);

    appWidgetManager.updateAppWidget(appWidgetIds, remoteViews );
}

private String[] getString() {
    String[] hon = new String[2];
    KamusWidget ad = new KamusWidget(null);
    ad.open();
    hon = ad.getwordday();
    ad.close();
    return hon;
}

@Override
public void onReceive(Context context, Intent intent) {
    // v1.5 fix that doesn't call onDelete Action
    final String action = intent.getAction();
    if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
        final int appWidgetId = intent.getExtras().getInt(
                AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);

        if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
            this.onDeleted(context, new int[] { appWidgetId });
        }
    } else {
        // check, if our Action was called
        if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
            String msg = "null";
            try {
                msg = intent.getStringExtra("msg");
            } catch (NullPointerException e) {
                Log.e("Error", "msg = null");
            }
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();


        } else {
            // do nothing
        }

        super.onReceive(context, intent);
    }
}

 }

我只想在点击 R.id.update 时更新 widget_title 和 widget_textview 它应该运行函数 getString 从其他类中获取字符串,以便将新字符串传递给 textview...

有什么想法吗?

【问题讨论】:

    标签: android widget timertask


    【解决方案1】:

    onReceive 函数的最后,您应该能够创建一个 RemoteView 并更新小部件:

    ComponentName name = new ComponentName(context, WordWidget.class);
    int[] ids = appWidgetManager.getAppWidgetIds(name);
    if(ids != null && ids.length > 0){
    RemoteView remoteViews = new RemoteViews( context.getPackageName(), R.layout.widget );
    remoteViews.setTextViewText( R.id.widget_title, context.getString(R.id.new_widget_title));
    remoteViews.setTextViewText( R.id.widget_textview, context.getString(R.id.new_widget_text));
    
    Intent configIntent = new Intent(context, SuitAuto.class);
    configIntent.setAction(ACTION_WIDGET_CONFIGURE);
    
    Intent active = new Intent(context, WordWidget.class);
    active.setAction(ACTION_WIDGET_RECEIVER);
    active.putExtra("msg", "Message for Button 1");
    
    PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
    PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
    
    remoteViews.setOnClickPendingIntent(R.id.update, actionPendingIntent);
    remoteViews.setOnClickPendingIntent(R.id.Lilo, configPendingIntent);
    
    appWidgetManager.updateAppWidget(ids, remoteViews );
    }
    

    我相信您还需要设置 Intent:如果您不这样做,则更新 RemoteView 后按钮将不再起作用。

    【讨论】:

    • 不清楚我们如何在您的示例中获取 appWidgetManager“变量”,我遇到了类似的问题,我通过远程视图更新了 textview,但它似乎并没有真正改变文本.
    • 如果你有一个上下文(任何上下文,例如活动或服务),你应该能够做到AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);:我在服务内部使用它来更新显示值的小部件蓝牙传感器。
    • 我已经阅读了stackoverflow.com/questions/23220757/… 并试图让它工作几天。最后我在上面的 AppWidgetManager 评论中找到了这个答案,我终于可以让它工作了。点赞!
    【解决方案2】:

    我相信

    Intent active = new Intent(context, WordWidget.class);
    active.setAction(ACTION_WIDGET_RECEIVER);
    

    应该是

    Intent active = new Intent(ACTION_WIDGET_RECEIVER);
    

    如果未使用简单字符串实例化其意图,则广播 PendingIntent 将不起作用。 setAction() 出于某种原因没有做同样的事情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 2018-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      相关资源
      最近更新 更多