问题
I have Widget that uses a service to update. It has button on it to open the activity. It works perfectly. However, sometimes i click on the widget to open the activity and it wont work or update.
My question is: what kills an app widget. And if the widget is dead, why is it still showing on the screen. Is there anyway to relive that specific widget without creating another.
Thanks
回答1:
Android will stop your widget when it faces a low memory situation. Then it will restart your home widget after cleaning the RAM automatically. But this time, your home widget will get a different pid from the last one, so it cannot respond the broadcast.
Updating app widget using AlarmManager
回答2:
I had to include this code in my service class. What it does basically it sends the update to every single widget. Its been over a year now, the button on my widget always works and the widget updates without any problem. I'm posting this hoping it will help someone with the same problem. It took me over a year to solve it.
int[] arrayOfInt = AppWidgetManager.getInstance(getBaseContext())
.getAppWidgetIds(new ComponentName(this, mywidget.class));
AppWidgetManager localAppWidgetManager = AppWidgetManager
.getInstance(this);
new WebView().onUpdate(getBaseContext(), localAppWidgetManager,
arrayOfInt);
int i = arrayOfInt.length;
for (int j = 0;; j++) {
if (j >= i)
return;
int k = arrayOfInt[j];
RemoteViews localRemoteViews = new RemoteViews(getBaseContext()
.getPackageName(), R.layout.xx);
localRemoteViews.setTextViewText(R.id.tv, tv_text);
localRemoteViews.setOnClickPendingIntent(R.id.Button01,
PendingIntent.getActivity(getBaseContext(), 0, new Intent(
getBaseContext(), LicenseCheck.class), 1));
localAppWidgetManager.updateAppWidget(k, localRemoteViews);
}
来源:https://stackoverflow.com/questions/11056332/widget-does-not-respond