RemoteViewsService 不会 运行
RemoteViewsService Wont run
我似乎无法理解为什么我的 onUpdate 和来自我的 AppWidgetProvider class 的 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;
}
};
}
}
我的问题的解决方案很奇怪。来回重构服务名称使其变为 运行。即使在比较 git 提交之后,我也无法判断重构发生了什么变化。但至少现在 运行s.
我似乎无法理解为什么我的 onUpdate 和来自我的 AppWidgetProvider class 的 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;
}
};
}
}
我的问题的解决方案很奇怪。来回重构服务名称使其变为 运行。即使在比较 git 提交之后,我也无法判断重构发生了什么变化。但至少现在 运行s.