【发布时间】:2012-02-16 12:53:23
【问题描述】:
所以,我开发了一个应该用作按钮的 android 小部件。我使用了这里给出的基本代码:http://developer.android.com/guide/topics/appwidgets/index.html 单击按钮时,将启动一个活动。这每次都很好用!但是,当我记录单击按钮的时间时,我只得到第一次。为什么会发生这种情况?
这是我被要求提供的代码:
public class ExampleAppWidgetProvider extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
Log.d("myButton","This is only called once.Why????????")
// Create an Intent to launch ExampleActivity
Intent intent = new Intent(context, ExampleActivity.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.appwidget_provider_layout);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
【问题讨论】:
-
没有看到您的代码,这是无法回答的。
-
我添加了一些代码。正如我已经提到的,这只是 android 教程
-
android 文档没有更改颜色或类似内容的功能。因此,您的代码必须与文档有所不同,并且必须以某种方式成为根本原因(我们需要查看该部分以帮助您)。
-
好的。忘记添加功能..我删除了。我只有一个 log.d() ,它每次都不会被调用。我的方法看起来完全像这样。但是,可以在另一个 widget_layout.xml 文件中添加按钮颜色等功能并调用而不是初始布局。
标签: android android-widget onupdate