我的小部件缩略图在 Android API 31 小部件列表中是空白的

My widget Thumbnail is blank in Android API 31 widgets list

我的应用程序有一个简单的小部件(显示大图像以启动应用程序) 它在 Android 28 下工作正常,但它与 API31 不同:要拖动的缩略图存在于小部件列表中,但(使用模拟器)显示为空白 space,如图所示 in picture here 我注意到 Android 31 中有关小部件的更改,但没有发现任何可以解释此问题的内容

代码元素: 清单

activity
            android:name="com.dom.my_App.myAppAppWidgetConfigureActivity"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
            </intent-filter>
        </activity>
        
        

AppWidgetConfigureActivity

public class myAppAppWidget extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them
        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        // When the user deletes the widget, delete the preference associated with it.
        for (int appWidgetId : appWidgetIds) {
            myAppAppWidgetConfigureActivity.deleteTitlePref(context, appWidgetId);
        }
    }

    @Override
    public void onEnabled(Context context) {
        // Enter relevant functionality for when the first widget is created
    }

    @Override
    public void onDisabled(Context context) {
        // Enter relevant functionality for when the last widget is disabled
    }

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                int appWidgetId) {

       // CharSequence widgetText = myAppAppWidgetConfigureActivity.loadTitlePref(context, appWidgetId);
        // Construct the RemoteViews object
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_App_app_widget);
        //  views.setImageViewBitmap (R.id.appwidget_text,
        views.setImageViewResource(R.id.widget_imageView, R.drawable.alerte_pm);

        Intent intent = new Intent(context, MainActivity.class);
       // views.setOnClickPendingIntent(R.id.widget_imageView, getPendingSelfIntent(PendingIntent.getActivity(
                PendingIntent pendingIntent = PendingIntent.getActivity(   /* context = */ context,
                /* requestCode = */ 0,
                /* intent = */ intent,
                /* flags = */ PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
        );
        views.setOnClickPendingIntent(R.id.widget_imageView, pendingIntent);

        // Instruct the widget manager to update the widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

    private static PendingIntent getPendingSelfIntent(Context context, String action) {
        Intent intent = new Intent(context.getApplicationContext(), MainActivity.class);

        //startActivity(intent);
        intent.setAction(action);
        return PendingIntent.getBroadcast(context, 0, intent, 0);
    }


}

App_widget

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@android:id/background"
    style="@style/Widget.my_SMS.AppWidget.Container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppWidgetContainer">

    <ImageView
        android:id="@+id/widget_imageView"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:background="@drawable/app_widget_inner_view_background"
        android:contentDescription="@string/app_name"
        android:cropToPadding="false"
        android:scaleType="fitCenter"
        app:srcCompat="@drawable/alerte_pm" />
</RelativeLayout>



app_wigzt_info

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:minWidth="80dp"
    android:minHeight="80dp"
    android:updatePeriodMillis="86400000"
    android:previewImage="@drawable/alerte_pm"
    android:previewLayout="@layout/my_App_app_widget">
    android:installLocation="preferExternal"
    android:initialLayout="@layout/my_App_app_widget"
    android:description="@string/app_name"
    android:configure="com.dom.my_App.myAppAppWidgetConfigureActivity"
    android:resizeMode="horizontal|vertical"
    android:widgetCategory="home_screen|keyguard"
    android:previewLayout="@layout/my_App_app_widget"
    android:targetCellHeight="2"
    android:targetCellWidth="2"
    android:initialKeyguardLayout="@layout/my_App_app_widget"
    tools:targetApi="s">
</appwidget-provider>

我找到了解决方案:它在 styles.xml (V31) 中:我介绍了这个项目

<item name="android:background">@drawable/my_drawable</item>

在最新版本的Android Studio中,通过添加新的activity Widget生成的文件也可以用于Android API 30.

这是理解要做的事情的方式。