我有一个你按下的小部件,然后它会更新小部件上的文本。我已经设置了一个点击监听器来启动另一个活动来执行文本更新,但由于某种原因它只能暂时工作,然后它将变得没有响应,并且在按下时不做任何事情。有谁知道它为什么会这样做?我发布了我的小部件代码以防万一它有帮助。android widget无响应
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
thisWidget = new ComponentName(context, MemWidget.class);
Intent intent = new Intent(context, updatewidget.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.ImageButton01, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current App Widget
appWidgetManager.updateAppWidget(thisWidget, views);
}
@Override
public void onReceive(Context context, Intent intent)
{
appWidgetManager = AppWidgetManager.getInstance(context);
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
thisWidget = new ComponentName(context, MemWidget.class);
// v1.5 fix that doesn't call onDelete Action
final String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
final int appWidgetId = intent.getExtras().getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
this.onDeleted(context, new int[] { appWidgetId });
}
}
else
{
super.onReceive(context, intent);
}
}
这里的代码是从我的活动称为
thisWidget = new ComponentName(this, MemWidget.class);
appWidgetManager = AppWidgetManager.getInstance(this);
remoteViews = new RemoteViews(this.getPackageName(), R.layout.widget);
//do work
remoteViews.setTextViewText(R.id.ImageButton01,"setting text here");
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
感谢您的回复。我在已启动活动的用户交互后添加了调用updateappwidget的代码。我将更改小部件以使用广播接收器,而不是启动不必要的活动。 在寻找解决方案时,我发现了这个答案“确保我将所有PendingIntents与每个RemoteViews更新一起发送,即使它们已经被设置。当方向更改时,布局充气且只有最近缓存RemoteViews应用于它。“ 但我该怎么做? – John 2010-06-15 15:06:33
谢谢,它工作! – John 2010-06-16 02:24:39