[英]My widget Thumbnail is blank in Android API 31 widgets list
My app has a simple widget (to show a large image to launch the app) It works fine under Android 28 but it is different with API31: the thumbnail to drag is present in widgets list, but shown (with emulator) by a blank space, as shown in picture here I noticed ther were changes in Android 31 concerning Widgets, but found nothing explaining this problem我的应用程序有一个简单的小部件(显示大图像以启动应用程序)它在 Android 28 下工作正常但它与 API31 不同:要拖动的缩略图存在于小部件列表中,但显示(使用模拟器)由空格,如图所示,我注意到 Android 31 中有关小部件的更改,但没有发现任何解释此问题的信息
Code elements: Manifest代码元素:清单
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 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 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>
I have found the solution: it was in styles.xml (V31): I introduced this item我找到了解决方案:它在 styles.xml (V31): I introduced this item
<item name="android:background">@drawable/my_drawable</item>
In the recent version of Android Studio, the files generated by adding a new activity Widget are OK also for Android API 30.在Android Studio的最新版本中,通过添加新的活动Widget生成的文件对于Android API 30也是可以的。
It was the way to understand the things to do.这是理解要做的事情的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.