简体   繁体   English

为什么我的应用小部件不会在API 3或4中更新?

[英]Why doesn't my app widget update in API 3 or 4?

I'm working on an android widget and it works great in API Level 5 or greater. 我正在开发一个Android小部件,它在API Level 5或更高版本中运行良好。 It's not supported at all in API Level 1 or 2. It should work absolutely fine in 3 and 4 but for some reason the widget doesn't update. 它在API级别1或2中根本不受支持。它应该在3和4中完全正常工作,但由于某种原因,小部件不会更新。

The onUpdate method gets called and executes without errors; onUpdate方法被调用并执行而没有错误; however, in 3 and 4 it doesn't change the text of the widget. 但是,在3和4中它不会更改小部件的文本。 I'm pretty much at a loss. 我几乎不知所措。 Any thoughts? 有什么想法吗?

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);

    DataAccess helper = new DataAccess(context);
    String text = helper.getCurrentText();
    helper.close();

    if (text != null)
        views.setTextViewText(R.id.widget_text, text);

    Intent intent = new Intent(context, WidgetDetailsActivity.class);
    PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
    views.setOnClickPendingIntent(R.id.widget, pending);

    appWidgetManager.updateAppWidget(appWidgetIds, views);
}

Try this: 试试这个:

At the end of your update method you need to call the super. 在更新方法结束时,您需要调用super。

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);

    DataAccess helper = new DataAccess(context);
    String text = helper.getCurrentText();
    helper.close();

    if (text != null)
        views.setTextViewText(R.id.widget_text, text);

    Intent intent = new Intent(context, WidgetDetailsActivity.class);
    PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
    views.setOnClickPendingIntent(R.id.widget, pending);

    appWidgetManager.updateAppWidget(appWidgetIds, views);

   super.onUpdate(context, appWidgetManager, appWidgetIds);
}

You need to start service on update method and put your all code in service also,here I give onReceive and service demo context.startService(new Intent(context, UpdateService.class)); 你需要在update方法上启动服务并将你的所有代码都投入使用,这里我给出onReceive和service demo context.startService(new Intent(context, UpdateService.class));

public static class UpdateService extends Service {
    @Override
    public void onStart(Intent intent, int startId) {
        // Build the widget update for today
        RemoteViews updateViews = buildUpdate(this);
        // Push update for this widget to the home screen
        ComponentName thisWidget = new ComponentName(this,
                Widget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(this);
        manager.updateAppWidget(thisWidget, updateViews);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

@Override
public void onReceive(Context context, Intent intent) {
    final RemoteViews remoteViews  = buildUpdate(context);
    ComponentName cn = new ComponentName(context, Widget.class);
    AppWidgetManager.getInstance(context).updateAppWidget(cn, remoteViews);
    super.onReceive(context, intent);
}

where buildUpdate is method for your widget updating code 其中buildUpdate是您的小部件更新代码的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM