3

当我尝试更新内容时,我的 App-Widget 出现问题。

小部件看起来像这样:http: //i.stack.imgur.com/tEGLv.jpg

Widget 由一个 Header (Buttons + TextViews) 和一个 ListView 组成。如果用户按下按钮,ListView 会显示第二天的内容,一切正常。但问题是,如果我尝试从我的应用程序更新小部件或 ListView 不会更新的自动时间段。

这是我的代码。

应用小部件提供者:

public class StudyWidgetProvider extends AppWidgetProvider{

private static String BUTTON_BACK = "BUTTON_BACK";
private static String BUTTON_FORWARD = "BUTTON_FORWARD";

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {  
    context.startService(new Intent(context, UpdateService.class));
}

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    if(WidgetViewsFactory.currentDate == null){
        WidgetViewsFactory.currentDate = Calendar.getInstance();
    }

    String action = intent.getAction();
    if(action != null){
        if(action.equals(BUTTON_BACK)){
            WidgetViewsFactory.currentDate.add(Calendar.DATE, -1);
        }
        if(action.equals(BUTTON_FORWARD)){
            WidgetViewsFactory.currentDate.add(Calendar.DATE, 1);
        }
    }

    context.startService(new Intent(context, UpdateService.class));
}   
 }

处理 Widget 更新的 UpdateService。

    @Override
@Deprecated
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    Log.i(LOG_TAG, "Update-Service started");

    RemoteViews widget = buildUpdate(this);
    ComponentName thisWidget = new ComponentName(this, StudyWidgetProvider.class);
    AppWidgetManager manager = AppWidgetManager.getInstance(this);

    manager.notifyAppWidgetViewDataChanged(manager.getAppWidgetIds(thisWidget), R.id.words);
    manager.updateAppWidget(thisWidget, widget);

    Log.i(LOG_TAG, "The Content!");
    if(widgetObjects == null){
        Log.i(LOG_TAG, "WidgetObjects sind NULL");
    } else {
        Log.i(LOG_TAG, "WidgetObjects sind NICHT-NULL");

        for(int i=0; i<widgetObjects.size(); i++){
            Log.i(LOG_TAG, "--- " + widgetObjects.get(i).getName());
        }
    }

    Log.i(LOG_TAG, "Update-Service ended");
}


public RemoteViews buildUpdate(Context context){
    RemoteViews retVal = new RemoteViews(context.getPackageName(), R.layout.widget);

    if(WidgetViewsFactory.currentDate == null){
        WidgetViewsFactory.currentDate = Calendar.getInstance();
    }

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    ComponentName thisAppWidget = new ComponentName(context.getPackageName(), StudyWidgetProvider.class.getName());
    int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);

    loadSDCardContent(context, appWidgetManager, appWidgetIds);

    //Setzen des Widget-UIs
    Intent intent = new Intent(context, WidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds);
    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

    retVal.setRemoteAdapter(R.id.words, intent);

    SimpleDateFormat dateFormater = new SimpleDateFormat("EEEE");
    retVal.setTextViewText(R.id.widget_day, dateFormater.format(WidgetViewsFactory.currentDate.getTime()));
    dateFormater = new SimpleDateFormat("dd.MM.yyyy");
    retVal.setTextViewText(R.id.widget_date, dateFormater.format(WidgetViewsFactory.currentDate.getTime()));

    //Button-Actions
    Intent backIntent = new Intent(context, StudyWidgetProvider.class);
    backIntent.setAction(BUTTON_BACK);
    Intent forwardIntent = new Intent(context, StudyWidgetProvider.class);
    forwardIntent.setAction(BUTTON_FORWARD);
    PendingIntent backPendingIntent = PendingIntent.getBroadcast(context, 0, backIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent forwardPendingIntent = PendingIntent.getBroadcast(context, 0, forwardIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    retVal.setOnClickPendingIntent(R.id.widget_but_back, backPendingIntent);
    retVal.setOnClickPendingIntent(R.id.widget_but_forward, forwardPendingIntent);

    Intent clickIntent = new Intent(context, WidgetActivity.class);
    PendingIntent clickPI=PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    retVal.setPendingIntentTemplate(R.id.words, clickPI);

    return retVal;
}

下面是我如何从我的应用程序更新小部件:

public RemoteViews buildUpdate(Context context){
    RemoteViews retVal = new RemoteViews(context.getPackageName(), R.layout.widget);

    if(WidgetViewsFactory.currentDate == null){
        WidgetViewsFactory.currentDate = Calendar.getInstance();
    }

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    ComponentName thisAppWidget = new ComponentName(context.getPackageName(), StudyWidgetProvider.class.getName());
    int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);

    loadSDCardContent(context, appWidgetManager, appWidgetIds);

    //Setzen des Widget-UIs
    Intent intent = new Intent(context, WidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds);
    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

    retVal.setRemoteAdapter(R.id.words, intent);

    SimpleDateFormat dateFormater = new SimpleDateFormat("EEEE");
    retVal.setTextViewText(R.id.widget_day, dateFormater.format(WidgetViewsFactory.currentDate.getTime()));
    dateFormater = new SimpleDateFormat("dd.MM.yyyy");
    retVal.setTextViewText(R.id.widget_date, dateFormater.format(WidgetViewsFactory.currentDate.getTime()));

    //Button-Actions
    Intent backIntent = new Intent(context, StudyWidgetProvider.class);
    backIntent.setAction(BUTTON_BACK);
    Intent forwardIntent = new Intent(context, StudyWidgetProvider.class);
    forwardIntent.setAction(BUTTON_FORWARD);
    PendingIntent backPendingIntent = PendingIntent.getBroadcast(context, 0, backIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent forwardPendingIntent = PendingIntent.getBroadcast(context, 0, forwardIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    retVal.setOnClickPendingIntent(R.id.widget_but_back, backPendingIntent);
    retVal.setOnClickPendingIntent(R.id.widget_but_forward, forwardPendingIntent);

    Intent clickIntent = new Intent(context, WidgetActivity.class);
    PendingIntent clickPI=PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    retVal.setPendingIntentTemplate(R.id.words, clickPI);

    return retVal;
}

奇怪的是,当我按下小部件的按钮时,一切都按我的意愿工作。我的列表视图的 onDatasetChanged 方法被调用并且内容正在改变,但是如果我从我的应用程序中尝试这个,没有任何反应

我希望你们中的某个人知道为什么这不起作用。我现在坚持了2天,我不知道......

感谢您的帮助!

编辑 我发现这不适用于我的设备(带有 miui v4 的 htc),而是在模拟器上。

编辑 2 我在我的设备上安装了 Android 4(带有 Sense 4),之后它也可以工作了。看起来 MIUI 是问题所在。

4

3 回答 3

7

我要解决这个问题,因为我遇到了同样的问题并找到了解决方案。

              manager.notifyAppWidgetViewDataChanged

这将调用 RemoteViewsFactory 中的 onDataSetChanged()。添加调用以在该方法中构建您的视图。这解决了我所有的问题。

于 2012-12-29T02:53:37.920 回答
1

仅通过广播与小部件通信。不要这样直接调用。如果需要,使用stickyBroadcasts。

于 2012-08-07T19:08:57.587 回答
0

这有效

 @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    for (int widgetId : appWidgetIds) {
        RemoteViews mView = initViews(context, appWidgetManager, widgetId);
        appWidgetManager.updateAppWidget(widgetId, mView);
    }

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



@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
private RemoteViews initViews(Context context,
                              AppWidgetManager widgetManager, int widgetId) {

    RemoteViews mView = new RemoteViews(context.getPackageName(),
            R.layout.widget_layout);

    Intent intent = new Intent(context, WidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);

    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
    mView.setRemoteAdapter(widgetId, R.id.widgetCollectionList, intent);

    return mView;
}
于 2016-11-04T08:40:22.397 回答