【问题标题】:How to update my android app widget listview in widget development如何在小部件开发中更新我的 android 应用小部件列表视图
【发布时间】:2013-03-13 06:49:46
【问题描述】:

我是 android 应用小部件开发的新手。 我想为我的应用程序创建应用程序小部件。

我在应用小部件开发中使用了这个 Github 项目。

https://github.com/commonsguy/cw-advandroid/tree/master/AppWidget/LoremWidget

要求:

1. 我的列表视图的数据是动态的,意味着数据与我的应用程序数据同步。如果应用程序数据发生变化,则自动更新/删除小部件数据。

我搜索了很多天,但没有得到任何解决方案。

如果有人有解决方案,请帮忙。

【问题讨论】:

    标签: android android-intent android-widget widget


    【解决方案1】:

    @Harshid 此代码可以帮助您。只需检查一下并尝试一下。

    package com.automatic.widget;
    
    import android.app.PendingIntent;
    import android.appwidget.AppWidgetManager;
    import android.appwidget.AppWidgetProvider;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.RemoteViews;
    
    public class Widget extends AppWidgetProvider {
    
    private static final String SYNC_CLICKED    = "automaticWidgetSyncButtonClick";
    
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        RemoteViews remoteViews;
        ComponentName watchWidget;
    
        remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        watchWidget = new ComponentName(context, Widget.class);
    
        remoteViews.setOnClickPendingIntent(R.id.sync_button, getPendingSelfIntent(context, SYNC_CLICKED));
        appWidgetManager.updateAppWidget(watchWidget, remoteViews);
    }
    
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        super.onReceive(context, intent);
    
        if (SYNC_CLICKED.equals(intent.getAction())) {
    
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    
            RemoteViews remoteViews;
            ComponentName watchWidget;
    
            remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
            watchWidget = new ComponentName(context, Widget.class);
    
            remoteViews.setTextViewText(R.id.sync_button, "TESTING");
    
            appWidgetManager.updateAppWidget(watchWidget, remoteViews);
    
        }
    }
    
    protected PendingIntent getPendingSelfIntent(Context context, String action) {
        Intent intent = new Intent(context, getClass());
        intent.setAction(action);
        return PendingIntent.getBroadcast(context, 0, intent, 0);
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-04
      • 1970-01-01
      • 1970-01-01
      • 2020-11-21
      • 2018-12-29
      相关资源
      最近更新 更多