我的应用中有2个小部件。两者都具有完全相同的功能,但具有不同的布局。 它们在每台设备上运行良好,但看起来三星设备上存在问题。每次我发布新版本的应用程序时,都会从用户屏幕中删除该窗口小部件。看起来这是一个三星TouchWiz错误,但用户告诉我其他应用程序没有这个问题。 知道发生了什么事吗?
BTW我想了一下我在这篇博文中找到了解决问题的方法:https://medium.com/the-wtf-files/the-mysterious-case-of-the-disappearing-widget-f4177a8e2c2b 但我不是手动打电话
updateIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
相反,我正在打电话
ComponentName cn = new ComponentName(context,clazz); AppWidgetManager.getInstance(context).updateAppWidget(cn,views);
为了更新我的小部件
我的更多代码:
public static final int layout = R.layout.widget_large_layout;
public static final BitmapQualityEnum thumbnailQuality = BitmapQualityEnum.HIGH_RES;
public static final Class<?> clazz = LargeWidgetProvider.class;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
if (context != null && appWidgetIds != null && appWidgetIds.length > 0) {
// To prevent any ANR timeouts, we perform the update in a service
context.startService( new Intent(context, LargeWidgetProvider.UpdateService.class));
}
}
@Override
public void onReceive(Context context, Intent intent) {
if (context != null && intent != null) {
boolean doUpdate = true;
if ("android.appwidget.action.APPWIDGET_UPDATE".equals(intent.getAction())) {
final int[] widgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
doUpdate = (widgetIds != null && widgetIds.length > 0);
}
if (doUpdate) {
WidgetProviderHelper.receive(context, intent, clazz, layout, thumbnailQuality);
}
super.onReceive(context, intent);
}
}
public static class UpdateService extends Service {
private int serviceStartId = -1;
@Override
public boolean onUnbind(Intent intent) {
stopSelf(this.serviceStartId);
return true;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
this.serviceStartId = startId;
// Push update for this widget to the home screen
updateData(this);
return START_STICKY;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// int oldOrientation = this.getResources().getConfiguration().orientation;
// if (newConfig.orientation != oldOrientation) {
// Update the widget) {
updateData(this);
// }
}
@Override
public IBinder onBind(Intent intent) {
// We don't need to bind to this service
return null;
}
}
答案 0 :(得分:2)
我终于设法解决了我的问题。 需要导出小部件提供程序接收器!!
<receiver
android:name=".widget.HorizontalWidgetProvider"
android:exported="true" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.customEvent" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_horizontal_provider" />
</receiver>