WidgetProvider类
public class WidgetProvider extends AppWidgetProvider {
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int widgetId : appWidgetIds) {
RemoteViews mView = initViews(context, appWidgetManager, widgetId);
appWidgetManager.updateAppWidget(widgetId, mView);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
private RemoteViews initViews(Context context, AppWidgetManager widgetManager, int widgetId) {
RemoteViews mView = new RemoteViews(context.getPackageName(), R.layout.row);
Intent intent = new Intent(context, WidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
mView.setRemoteAdapter(widgetId, R.id.textViewNoteTitle, intent);
return mView;
}
}
WidgetService类
public class WidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
WidgetDataProvider dataProvider = new WidgetDataProvider(getApplicationContext(), intent);
return dataProvider;
}
}
WidgetDataProvider类
public class WidgetDataProvider implements RemoteViewsFactory {
ArrayList mCollections = new ArrayList();
Context mContext = null;
public WidgetDataProvider(Context context, Intent intent) {
mContext = context;
}
@Override
public int getCount() {
return mCollections.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public RemoteViews getLoadingView() {
return null;
}
@Override
public RemoteViews getViewAt(int position) {
RemoteViews mView = new RemoteViews(mContext.getPackageName(), R.layout.activity_main);
mView.setTextViewText(R.id.gridView, (CharSequence) mCollections.get(position));
mView.setTextColor(R.id.gridView, Color.BLACK);
return mView;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public void onCreate() {
initData();
}
@Override
public void onDataSetChanged() {
initData();
}
private void initData() {
mCollections.clear();
for (int i = 1; i <= 10; i++) {
mCollections.add("ListView item " + i);
}
}
@Override
public void onDestroy() {
}
Widget_provider xml
我在加载内容和自定义行时遇到错误,我正在使用文本和图片。