未显示在小部件列表中的小部件 有一个类似的问题,还有许多其他问题的答案基本上是:重启、启动应用程序、等待等。
应用安装后,它会在 Android 2.3 设备上显示小部件。所以代码很好。但它从未出现在 4.3 设备上。所以 4.3 正在寻找不存在的东西。
有没有人有任何额外的提示?
AndroidManifest.xml
<receiver
android:name=".WidgetProvider"
android:label="@string/widget_name">
<intent-filter>
<action
android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
</receiver>
小部件信息.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" >
android:minWidth="208dp"
android:minHeight="56dp"
android:minResizeHeight="48dp"
android:minResizeWidth="48dp"
android:updatePeriodMillis="86400000"
android:previewImage="@drawable/ic_launcher"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen"
android:configure=""
android:initialLayout="@layout/widget_layout"
</appwidget-provider>
WidgetProvider.java
public class WidgetProviderextends AppWidgetProvider {
DateFormat df = new SimpleDateFormat("hh:mm:ss");
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
Log.i("ExampleWidget", "Updating widgets " + Arrays.asList(appWidgetIds));
// Perform this loop procedure for each App Widget that belongs to this
// provider
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
// Create an Intent to launch ExampleActivity
Intent intent = new Intent(context, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
views.setOnClickPendingIntent(R.id.new_post, pendingIntent);
// To update a label
views.setTextViewText(R.id.blogname, df.format(new Date()));
// Tell the AppWidgetManager to perform an update on the current app
// widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
上面的所有代码主要是用于测试的示例代码,它在 Android 2.3 上运行良好,但在 4.3 上运行良好......在 logcat 输出中运行调试构建时没有错误。