Widget becomes a different widget from another app [duplicate]
I may be doing something stupid, but I have an interesting problem with my app widget. Every now and then, often after being added to the homescreen, it transforms into a widget from another app. This widget is always the same on a specific phone, but differs between phones. After a few seconds, the widget then returns back to how it should look. Although for me this isn't an issue as it is only a temporary problem, I have emails from users who say the widget is permanently stuck in another form.
Below are any files which could be relevant:
- The widget provider
- The widget configuration activity
- The widget xml layout
- Xml layout item
- Manifest
I'm sorr开发者_运维问答y to attach so many but I really don't know where to start. I am hoping that to someone more familiar with the api the problem will be immediately apparent. I also apologize for any poor code as this is my first ever serious project and is just a hobby alongside schoolwork.
I had the exact same issue in my widget as you described and searched a lot for a solution and couldn't find any. What I ended up doing is some kind of workaround that seems to work fine in my case. What i did is the following, instead of updating the widgets directly from the onUpdate() method, I started a service that handled the update then killed itself. This solved it on the emulator and on a device that had the widget stuck during update. Here's a sample code:
The AppWidgetProvider:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
context.startService(new Intent(context, WidgetService.class));
}
The Service:
@Override
public void onStart(Intent intent, int startId) {
started(intent);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
started(intent);
return START_STICKY;
}
private void started(Intent intent) {
//update here the widgets
Context context = getApplicationContext();
updateWidgets(context);
stopSelf();//killing the service
}
private void updateWidgets(Context context) {
AppWidgetManager appmanager = AppWidgetManager.getInstance(context);
ComponentName cmpName = new ComponentName(context, widgetClass);
int[] widgetIds = appmanager.getAppWidgetIds(cmpName);
RemoteViews rView = new RemoteViews(context.getPackageName(), layoutId);
for (int wid : widgetIds) {
//all updates here
rView.setTextViewText(tvId, desc);
appmanager.updateAppWidget(wid, rView);
}
}
Note sure why this workaround solves the issue, but the good thing is that it does Hope this helps
精彩评论