何かが足りないと思いますが、ボタンとカウンターを備えたアプリウィジェットを取得しようとしています。ボタンをクリックするたびに、カウンターを1ずつ更新します。
WidgetProviderのonUpdate()関数を設定して、保留中のイベントをボタンに登録し、カウンターの数を増やすサービスを開始します。
Intent active = new Intent(context, CounterService.class);
active.setAction(CounterService.COUNT);
PendingIntent pending = PendingIntent.getService(context, 0, active, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.CountButton, pending);
ComponentName component = new ComponentName(context.getPackageName(), KickCounterWidgetProvider.class.getName());
appWidgetManager.updateAppWidget(component, views);
次に、サービスのCounterService :: onStart()関数で、カウントを増やし(今のところ設定に保存されています)、現在のカウント値を示すテキストフィールドを更新してみます。
// ... bump the count here and store a string representation of it in currentCountString ...
RemoteViews remoteView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.widget);
remoteView.setTextViewText(R.id.CurrentKickCount, currentCountString);
// apply changes to widget
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
ComponentName component = new ComponentName(getApplicationContext().getPackageName(), KickCounterWidgetProvider.class.getName());
appWidgetManager.updateAppWidget(component, remoteView);
Logcatの賢明な使用は、これがすべて正常に機能し、文字列が正常であるように見えることを示していますが、何らかの理由でappWidgetManager.updateAppWidget()の呼び出しが何らかの理由でサイレントに失敗しているようです。
関連しているかどうかはわかりませんが、ウィジェットのインスタンスをホーム画面に初めて追加すると、ボタンも機能しません(つまり、プロバイダーのonUpdate()でupdateAppWidget()を呼び出すと失敗します)。ウィジェットの後続のインスタンスは、プロバイダーのupdateAppWidget()の呼び出しでは正常に機能しているように見えますが、サービスでは機能しません。
どんな助けでも大歓迎です。