RemoteViewsService不会运行

时间:2017-04-19 16:11:28

标签: android widget appwidgetprovider

我似乎无法理解为什么我的onUpdate和来自AppWidgetProvider类的onRecieve都没有启动我的remoteviewservice。 onUpdate和onRecieve都运行。收件人和服务都在清单中声明。

这是我的提供者

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);
    }
}

}

这是我的服务

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;
        }
    };
}

}

1 个答案:

答案 0 :(得分:0)

我的问题的解决方案很奇怪。反复重构服务名称使其运行。即使比较了git提交,我也无法告诉重构的变化。但至少现在它已经运行了。