#android #widget #uistackview
#Android #виджет #uistackview
Вопрос:
Я уверен, что здесь я упускаю что-то простое, но onGetViewFactory не вызывается при попытке заполнить StackWidget. Я просматривал это несколько раз и не могу найти свою ошибку.
Служба RemoteViewsService подключается в updateWidget:
public static void updateWidget(Context context, AppWidgetManager appWidgetManager, int widgetId) {
// Create the RemoteViews to represent the widget layout.
Log.i(TAG, "updateWidget: Creating RemoteViews");
RemoteViews widgetViews = new RemoteViews(context.getPackageName(), R.layout.stack_widget_layout);
// Create an intent that points to our widget service.
Log.i(TAG, "updateWidget: Creating Intent");
Intent remoteIntent = new Intent(context, StackWidgetService.class);
// Attach that intent to the RemoteViews as our remote adapter.
Log.i(TAG, "updateWidget: Setting Adapter");
widgetViews.setRemoteAdapter(R.id.stack_view, remoteIntent);
// Set which view should be used as the empty view.
Log.i(TAG, "updateWidget: Setting empty view");
widgetViews.setEmptyView(R.id.stack_view, R.id.text_empty);
// Update the widget.
Log.i(TAG, "updateWidget: Updating widget");
appWidgetManager.updateAppWidget(widgetId, widgetViews);
}
После вызова AppWidgetManager должно быть вызвано следующее.updateAppWidget но не:
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
// NOT CALLED
Log.i(TAG, "onGetViewFactory: Creating new view factory");
return new StackWidgetViewFactory(getApplicationContext());
}
Насколько я могу судить, мой манифест правильный:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".stackwidget.StackWidgetProvider"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/stack_widget_info" />
</receiver>
<service android:name=".stackwidget.StackWidgetService"
android:exported="true"
android:permission="android.permission.BIND_REMOTEVIEWS"/>
</application>
И вот мой stack_widget_info.xml для безопасности:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="250dp"
android:minHeight="180dp"
android:updatePeriodMillis="1800000"
android:initialLayout="@layout/stack_widget_layout"
android:autoAdvanceViewId="@layout/stack_item"
android:widgetCategory="home_screen"
android:resizeMode="horizontal |vertical">
</appwidget-provider>
Комментарии:
1. Здесь то же самое. onGetViewFactory по какой-то причине просто не вызывается на некоторых устройствах, но чудесным образом начинает работать после переустановки того же APK. Я считаю, что это особенность их PackageManager, но все еще не могу найти способ исправить это или объяснить пользователям, почему виджет не обновляется.
2. @avs333 Вы нашли какое-либо решение для этого, я тоже сталкиваюсь с тем же. При второй установке onGetViewFactory вызывается, но не для первой.
Ответ №1:
Проблема была на самом деле в моем макете виджета. Компоновка ограничений вызывала проблемы. Он работает корректно при переходе на линейную компоновку.