不会调用RemoteViewsService onGetViewFactory

时间:2019-03-17 02:45:25

标签: android widget uistackview

我确定这里缺少一些简单的东西,但是在尝试填充StackWidget时onGetViewFactory没有调用。我已经经历了多次,找不到错误。

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 个答案:

答案 0 :(得分:0)

问题实际上出在我的小部件布局中。约束布局导致问题。切换为线性布局时,它可以正常运行。