为什么我的应用程序小部件没有在 API 3 或 4 中更新?

发布于 2024-10-05 05:27:56 字数 859 浏览 4 评论 0原文

我正在开发一个 Android 小部件,它在 API 级别 5 或更高版本中工作得很好。它在 API 级别 1 或 2 中根本不受支持。它在 3 和 4 中应该工作得绝对正常,但由于某种原因,小部件不会更新。

onUpdate 方法被调用并且执行时没有错误;但是,在 3 和 4 中,它不会更改小部件的文本。我几乎不知所措。有什么想法吗?

@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);
}

I'm working on an android widget and it works great in API Level 5 or greater. 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.

The onUpdate method gets called and executes without errors; however, in 3 and 4 it doesn't change the text of the widget. 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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌上青苔 2024-10-12 05:27:56

试试这个:

在更新方法结束时,您需要调用 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);
}

Try this:

At the end of your update method you need to call the 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);
}
泪是无色的血 2024-10-12 05:27:56

您需要在更新方法上启动服务并将所有代码也放入服务中,这里我给出 onReceive 和服务演示
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);
}

其中 buildUpdate 是小部件更新代码的方法

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));

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文