I cant seem to understand why neither my onUpdate nor onRecieve from my AppWidgetProvider class starts my remoteviewservice. Both onUpdate and onRecieve does run. And both the reciever and the service are declared in the manifest.
This is my provider
public class WidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Timber.d("onUpdate");
for (int appWidgetId : appWidgetIds) {
Timber.d("onUpdate" + appWidgetId);
Intent intent = new Intent(context, WidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.stock_widget);
rv.setRemoteAdapter(R.id.stock_list, intent);
Intent stockHawkActivityIntent = new Intent(context, MainActivity.class);
PendingIntent activityPendingIntent = PendingIntent.getActivity(context, 0, stockHawkActivityIntent, 0);
rv.setPendingIntentTemplate(R.id.stock_list, activityPendingIntent);
rv.setEmptyView(R.id.stock_list, R.id.empty_view);
appWidgetManager.updateAppWidget(appWidgetId, rv);
}
}
@Override
public void onReceive(Context context, Intent intent) {
Timber.d("onRecive");
super.onReceive(context, intent);
if(QuoteSyncJob.ACTION_DATA_UPDATED.equals(intent.getAction())){
Timber.d("Data was updated");
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(
new ComponentName(context, getClass()));
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.stock_list);
}
}
}
and this is my service
public class WidgetService extends RemoteViewsService {
private static final String[] COLUMNS = Contract.Quote.QUOTE_COLUMNS.toArray(new String[]{});
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
Timber.d("Remote Views Facotry");
return new RemoteViewsFactory() {
private Cursor data = null;
@Override
public void onCreate() {
}
@Override
public void onDataSetChanged() {
Timber.d("Data was changed");
if (data != null) {
data.close();
}
final long identityToken = Binder.clearCallingIdentity();
Uri stocksUri = Contract.Quote.URI;
data = getContentResolver().query(stocksUri, COLUMNS, null, null, null);
Binder.restoreCallingIdentity(identityToken);
}
@Override
public void onDestroy() {
if (data != null) {
data.close();
data = null;
}
}
@Override
public int getCount() {
Timber.d("getCount: " + data.getCount());
return data == null ? 0 : data.getCount();
}
@Override
public RemoteViews getViewAt(int position) {
Timber.d("GetViewAt " + position);
if (position == AdapterView.INVALID_POSITION ||
data == null || !data.moveToPosition(position)) {
return null;
}
RemoteViews views = new RemoteViews(getPackageName(),
R.layout.widget_list_item);
String symbol = data.getString(Contract.Quote.POSITION_SYMBOL);
Float price = data.getFloat(Contract.Quote.POSITION_PRICE);
Float absChange = data.getFloat(Contract.Quote.POSITION_ABSOLUTE_CHANGE);
//Float prsChange = data.getFloat(Contract.Quote.POSITION_PERCENTAGE_CHANGE);
views.setTextViewText(R.id.symbol, symbol);
views.setTextViewText(R.id.price, price.toString());
views.setTextViewText(R.id.change, absChange.toString());
return views;
}
@Override
public RemoteViews getLoadingView() {
return new RemoteViews(getPackageName(), R.layout.widget_list_item);
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public long getItemId(int position) {
Timber.d("GetItemId: " + position);
if (data.moveToPosition(position))
return data.getLong(Contract.Quote.POSITION_ID);
return position;
}
@Override
public boolean hasStableIds() {
return false;
}
};
}
}
The solution to my problem was a weird one. Refactoring the name of the service back and forth got it to run. I cant tell what the refactoring changed even after comparing git commits. But at least now it runs.