【发布时间】:2011-08-12 22:40:03
【问题描述】:
我有一个简单的小部件 (Android 2.1),它只包含一个 LinearLayout,它本身包含一个 ImageButton。 ImageButton 有一个点击监听器。 问题是:如果我在我的主屏幕上放置几个相同的小部件,一些正在工作(按下按钮时调用监听器),而另一些则没有!我看不到任何有效和无效的模式。
这是小部件布局:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ImageButton01"
android:src="@drawable/widget_running"
android:background="@null">
</ImageButton>
这是小部件提供程序代码:
public class GPAppWidgetProvider extends AppWidgetProvider {
private String mTag = getClass().getSimpleName();
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
Log.e(mTag, "onUpdate ");
// Perform this loop procedure for each App Widget that belongs to this
// provider
for (int i = 0; i < N; i++) {
Log.e(mTag, "widget onUpdate one loop");
int appWidgetId = appWidgetIds[i];
// Create an Intent
Intent intent = new Intent(context, GPService.class).setAction(ACTION_WIDGET_TOGGLE_PAUSE);
intent.putExtra("widgetId", appWidgetId);
PendingIntent pauseIntent = PendingIntent.getService(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.gp_appwidget);
views.setOnClickPendingIntent(R.id.ImageButton01, pauseIntent);
// widget update
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
【问题讨论】:
-
更新:似乎如果我重新启动手机或应用程序,那么所有小部件都可以工作......但是如果我添加新的小部件,大多数情况下它们将无法工作,直到我重新启动手机或重启应用...
-
我也遇到了这种情况,一旦我添加了主屏幕小部件,它不会立即将侦听器附加到按钮上。我必须通过主页图标运行服务/启动应用程序才能触发更新。