Android 通过单击按钮更新 Widget

发布于 2024-09-17 17:04:35 字数 124 浏览 3 评论 0原文

我有一个小部件,显示数据库中的一些信息。该小部件每隔一小时定期更新一次。但我还允许用户通过单击小部件上的刷新按钮来手动更新它。如何执行单击操作并刷新小部件?

注意:小部件使用服务来执行操作。

提前致谢。

I have a widget that shows some informations from database. The widget is periodically updated every one hour. But i also let user to update it manually by clicking a refresh button on widget.How can i perform the click action and refresh the widget?

Note: The widget uses service to perform operations.

Thanx in advance.

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

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

发布评论

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

评论(2

雨的味道风的声音 2024-09-24 17:04:35

我花了几个小时寻找答案,终于找到了答案。在您的AppWidgetProvider中,在onUpdate中,循环您的小部件,并为每个小部件创建一个Intent(向其中添加小部件id和一些数据以使其唯一,例如意图uri),然后创建一个PendingIntent并将其分配给要在其中创建点击侦听器的视图。

    for(int i = 0; i<appWidgetIds.length; i++) {
        Intent serviceIntent = new Intent(context, MyService.class);
        serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
        serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)));
        PendingIntent pendingServiceIntent = PendingIntent.getService(context, 0, serviceIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        views.setOnClickPendingIntent(R.id.refresh, pendingServiceIntent);

        context.startService(serviceIntent);
        appWidgetManager.updateAppWidget(appWidgetIds[i], views);
    }

在我的具体情况下,我有两个单击侦听器,一个用于刷新,一个用于启动设置活动,因此如果您想启动一个活动,只需使用 PendingIntent.getActivity 并添加 Intent.FLAG_ACTIVITY_NEW_TASK 标志。

两者结合起来:

    for(int i = 0; i<appWidgetIds.length; i++) {
        Intent serviceIntent = new Intent(context, MyService.class);
        serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
        serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)));
        PendingIntent pendingServiceIntent = PendingIntent.getService(context, 0, serviceIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Intent settingsIntent = new Intent(context, SettingsActivity.class);
        settingsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        settingsIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
        settingsIntent.setData(Uri.parse(settingsIntent.toUri(Intent.URI_INTENT_SCHEME)));
        PendingIntent pendingSettingsIntent = PendingIntent.getActivity(context, 0, settingsIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        views.setOnClickPendingIntent(R.id.title, pendingSettingsIntent);
        views.setOnClickPendingIntent(R.id.refresh, pendingServiceIntent);

        context.startService(serviceIntent);
        appWidgetManager.updateAppWidget(appWidgetIds[i], views);
    }

希望这会对某人有所帮助。 :)

I've looked for an answer for hours and finally I've figured it out. In your AppWidgetProvider, in the onUpdate, loop in your widgets and, for each widget, create an Intent (add to it the widget id and some data to make it unique, like the intent uri), then create a PendingIntent and assign it to the view where you want to create the click listener.

    for(int i = 0; i<appWidgetIds.length; i++) {
        Intent serviceIntent = new Intent(context, MyService.class);
        serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
        serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)));
        PendingIntent pendingServiceIntent = PendingIntent.getService(context, 0, serviceIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        views.setOnClickPendingIntent(R.id.refresh, pendingServiceIntent);

        context.startService(serviceIntent);
        appWidgetManager.updateAppWidget(appWidgetIds[i], views);
    }

In my specific case I had two click listener, one to refresh and one to start the settings activity, so if you want to start an Activity just use the PendingIntent.getActivity and add the Intent.FLAG_ACTIVITY_NEW_TASK flag.

The two combined:

    for(int i = 0; i<appWidgetIds.length; i++) {
        Intent serviceIntent = new Intent(context, MyService.class);
        serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
        serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)));
        PendingIntent pendingServiceIntent = PendingIntent.getService(context, 0, serviceIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Intent settingsIntent = new Intent(context, SettingsActivity.class);
        settingsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        settingsIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
        settingsIntent.setData(Uri.parse(settingsIntent.toUri(Intent.URI_INTENT_SCHEME)));
        PendingIntent pendingSettingsIntent = PendingIntent.getActivity(context, 0, settingsIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        views.setOnClickPendingIntent(R.id.title, pendingSettingsIntent);
        views.setOnClickPendingIntent(R.id.refresh, pendingServiceIntent);

        context.startService(serviceIntent);
        appWidgetManager.updateAppWidget(appWidgetIds[i], views);
    }

Hope this will help someone. :)

想你只要分分秒秒 2024-09-24 17:04:35

我认为当用户单击*刷新按钮*n时,您必须重新加载小部件视图

//for refresh button
Intent refresh = new Intent(ctxt, WatchWidget_Honeycomb.class);
PendingIntent refreshIntent = PendingIntent.getBroadcast(ctxt, 0, refresh, 0);
widget.setOnClickPendingIntent(R.id.btn_Refresh, refreshIntent); 
appWidgetManager.updateAppWidget(new ComponentName(ctxt,WatchWidget_Honeycomb.class), widget);

如果您遇到任何问题,请发表评论。

快乐编码。

I think you have to reload your widget view when user click on *refresh butto*n.

//for refresh button
Intent refresh = new Intent(ctxt, WatchWidget_Honeycomb.class);
PendingIntent refreshIntent = PendingIntent.getBroadcast(ctxt, 0, refresh, 0);
widget.setOnClickPendingIntent(R.id.btn_Refresh, refreshIntent); 
appWidgetManager.updateAppWidget(new ComponentName(ctxt,WatchWidget_Honeycomb.class), widget);

If You are getting any problem then put comment.

Happy Coding.

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