我在Android上遇到Appwidget问题。我有一个listView和一个Button,Widget的更新周期设置为30分钟。 现在我想在单击按钮时更新listView。我在互联网上搜索过这个问题,但我还没有找到解决问题的答案。 在我的代码中,我需要在onReceived()方法中编写什么来更新小部件?
我的代码:
public class WidgetProvider extends AppWidgetProvider {
private static final String ACTION_UPDATE_CLICK = "click";
private Date date;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i = 0; i < N; ++i) {
RemoteViews remoteViews = updateWidgetListView(context,
appWidgetIds[i]);
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
remoteViews.setOnClickPendingIntent(R.id.bMorgenWidget,
getPendingSelfIntent(context,
ACTION_UPDATE_CLICK));
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
private PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, getClass());
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (ACTION_UPDATE_CLICK.equals(intent.getAction())) {
}
}
private RemoteViews updateWidgetListView(Context context, int appWidgetId) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
Intent svcIntent = new Intent(context, WidgetService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(appWidgetId, R.id.listViewWidget,
svcIntent);
remoteViews.setEmptyView(R.id.listViewWidget, R.id.empty_view);
return remoteViews;
}
}
答案 0 :(得分:0)
我遇到了同样的问题。我想知道如何强制更新我的小部件中的数据和ui。
我发现的解决方案是使用公共静态内部服务:
/**
* Update Big Widget Service
*/
public static class WidgetBigUpdateService extends Service
{
public WidgetBigUpdateService()
{
super();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Context context = getBaseContext();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, this.getClass()));
RemoteViews bigWidgetView = new RemoteViews(context.getPackageName(), R.layout.widget_big_layout);
refreshBigWidgetData(context, appWidgetManager, appWidgetIds, bigWidgetView);
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent)
{
return null;
}
}
方法
refreshBigWidgetData(context, appWidgetManager, appWidgetIds, bigWidgetView);
属于小部件,负责我的小部件的UI更新:
/**
* Asks for Big Widget Updates
*
* @param context
*/
private void widgetBigForceUpdate(Context context)
{
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, this.getClass()));
onUpdate(context, appWidgetManager, appWidgetIds);
}
正如您所看到的,它使用上下文,appWidgetManager和appWidgetId调用函数onUpdate()
。此代码100%正常运行,我毫不怀疑您可以根据具体用途进行调整。
希望这有帮助