我通过使用RemoteViewsFactory和RemoteViewsService在我的小部件中创建listView但是我有问题总是我的小部件是空的,当我对我的代码进行调试时我注意到onGetViewFactory方法没有调用从来没有我审查我的代码很多但我我的代码中看不出有什么问题。
填写列表视图RemoteViewsFactoryc:
public class RemoteViewsFactoryc implements RemoteViewsService.RemoteViewsFactory {
@Override
public void onCreate() {
//for test
Toast.makeText(mContext,"hellow",Toast.LENGTH_LONG).show();
}
}
WidgetService调用RemoteViewsFactoryc:
public class WidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new RemoteViewsFactoryc(this.getApplicationContext(), intent);
}
}
FootballWidget类:
public class FootballWidget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; i++) {
RemoteViews widget = new RemoteViews(context.getPackageName(),
R.layout.list_widget);
Intent intent = new Intent(context, WidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetIds[i]);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
widget.setRemoteAdapter(R.id.scores_list,intent);
widget.setEmptyView(R.id.scores_list, R.id.empty_view);
appWidgetManager.updateAppWidget(appWidgetIds[i], widget);
}
}
}
@Override
public void onReceive( Context context, Intent intent) {
super.onReceive(context, intent);
if (scoresAdapter.ACTION_DATA_UPDATED.equals(intent.getAction())) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(
new ComponentName(context, getClass()));
RemoteViews widget = new RemoteViews(context.getPackageName(),
R.layout.list_widget);
widget.setRemoteAdapter(R.id.scores_list,intent);
}
}
}
的AndroidManifest.xml:
<receiver
android:name=".FootballWidget"
android:icon="@drawable/fottballicon"
android:label="hi">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
<action android:name="com.example.android.football.app.ACTION_DATA_UPDATED"></action>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/app_widget_provider"></meta-data>
</receiver>
<service
android:name="barqsoft.footballscores.WidgetService"
android:enabled="true"
android:exported="false"
android:permission="android.permission.BIND_REMOTEVIEWS" />