StackViewが含まれているAppWidgetがあります。AppWidgetに加えて、ユーザーがAppWidgetをホーム画面に追加したときに開始されるサービスがあります。このサービスは2時間ごとに新しいデータをポーリングします。サービスが新しいデータを見つけたら、AppWidgetにアラートを送信し、そのデータをRemoteViewsService(RemoteViewsFactoryがあります)に渡して、新しいデータに必要なビューを作成できるようにする必要があります。
現在、サービスが新しいデータを検出すると、新しいデータが検出されたことをAppWidgetにブロードキャストし、そのデータをインテント内の整数の配列として渡します。
AppWidgetProviderのonReceiveメソッドがそのデータを引き出し、次にAppWidgetのRemoteViewに渡される新しいインテントを設定するプロセスを実行します。以下のサンプルコード:
public void onReceive( Context context, Intent intent )
{
int[] appWidgetIds = appWidgetManager.getAppWidgetIds( new ComponentName( context, SampleAppWidgetProvider.class ) );
int[] sampleData = intent.getIntArrayExtra( Intent.EXTRA_TITLE );
for ( int i = 0; i < appWidgetIds.length; i++ )
{
// RemoteViewsFactory service intent
Intent remoteViewsFactoryIntent = new Intent( context, SampleAppWidgetService.class );
remoteViewsFactoryIntent.putExtra( AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i] );
remoteViewsFactoryIntent.setData( Uri.parse( remoteViewsFactoryIntent.toUri( Intent.URI_INTENT_SCHEME ) ) );
remoteViewsFactoryIntent.putExtra( Intent.EXTRA_TITLE, sampleData );
RemoteViews rv = new RemoteViews( context.getPackageName(), R.layout.widget_layout );
// Sync up the remoteviews factory
rv.setRemoteAdapter( appWidgetIds[i], R.id.sample_widget, remoteViewsFactoryIntent );
rv.setEmptyView( R.id.sample_widget, R.id.empty_view );
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
}
super.onUpdate( context, appWidgetManager, appWidgetIds );
}
これは初めて動作します。データはRemoteViewsService/RemoteViewsFactoryによって表示されます。後続の新しいデータは、そのファクトリのサービスがすでに実行されているため、RemoteViewsFactoryのコンストラクタにヒットしません。
データを更新するにはどうすればよいですか?onDataSetChangedを使用する必要があるように感じますが、AppWidgetのonReceiveから渡したインテントにアクセスするにはどうすればよいですか?
これを適切に行う方法についての洞察をいただければ幸いです。ありがとう。