不知道这是否会被另一个线程回答,但是在寻找答案的一段时间内,我没有找到答案。
我正在实现一个应用程序小部件,以每 3000 万次显示一些文本,但在我单击小部件之前它没有更新。这是我的 onUpdate 方法:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
// Get all ids
ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
Calendar getToday = Calendar.getInstance();
for (int widgetId : allWidgetIds)
{
int number = (new Random().nextInt(100));
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
// Set the text
String n = ""+number;
remoteViews.setTextViewText(R.id.update, n);
remoteViews.setTextColor(R.id.update, Color.BLUE);
// Register an onClickListener
Intent intent = new Intent(context, MyWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
我的代码有什么问题?谁能帮帮我。提前致谢
添加 :
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:updatePeriodMillis="110000"
android:initialLayout="@layout/widget_layout"
android:minHeight="72dp"
android:minWidth="300dp">
</appwidget-provider>