2

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

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

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

要求:

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

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

如果有人有解决方案,请提供帮助。

4

1 回答 1

0

@Harshid This code can help you .Just check it out and try this.

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);
}
}
于 2014-04-04T06:39:32.960 回答