Hello,
I have a widget, that behaves awkwardly on 3rd party app updates/
installs from Android Market. It looks like the onReceive method gets
called and reinstantiates the widget but it does not execute the
onUpdate() method within and the widget is not clickable anymore. Any
ideas what might be wrong with my code and how to fix it? The widget
is not supposed to react to 3rd party app events at all, but some
suggested to me to listen to these intents and do nothing, so I added
them into my code.
The widget code:
public class MyAppWidget extends AppWidgetProvider {
private static final String TAG = "MyAppWidget";
public static final String SHOW_THREADS = "com.me.SHOW_THREADS";
public static final String ACTION_WIDGET_UPDATE =
"android.appwidget.action.APPWIDGET_UPDATE";
private static final int FLAG = 95767;
@Override
public void onUpdate(Context context, AppWidgetManager
appWidgetManager, int[] appWidgetIds) {
final RemoteViews remoteViews = new
RemoteViews(context.getPackageName(), R.layout.widget);
//
Intent launch = new Intent(context, MyAppWidget.class);
launch.setAction(SHOW_THREADS);
PendingIntent launchPI = PendingIntent.getBroadcast(context,
0, launch, 0);
remoteViews.setOnClickPendingIntent(R.id.widget_area,
launchPI);
Intent update = new Intent(context, MyAppWidget.class);
update.setAction(ACTION_WIDGET_UPDATE);
PendingIntent.getBroadcast(context, 0, update, 0);
final int ids = appWidgetIds.length;
if(ids>0) {
int count = Metyhod.getCount(context).code;
if (count>0) {
remoteViews.setTextViewText(R.id.widget_counter_text,
Integer.toString(count));
remoteViews.setViewVisibility(R.id.widget_counter, 0);
} else {
remoteViews.setTextViewText(R.id.widget_counter_text,
"0");
remoteViews.setViewVisibility(R.id.widget_counter, 8);
}
} else {
remoteViews.setTextViewText(R.id.widget_counter_text, "0");
remoteViews.setViewVisibility(R.id.widget_counter, 8);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
//
@Override
public void onReceive(Context context, Intent intent) {
final RemoteViews remoteViews = new
RemoteViews(context.getPackageName(), R.layout.widget);
final AppWidgetManager appWidgetManager =
AppWidgetManager.getInstance(context);
final ComponentName thisAppWidget = new
ComponentName(context.getPackageName(), MyAppWidget.class.getName());
final int[] appWidgetIds =
appWidgetManager.getAppWidgetIds(thisAppWidget);
onUpdate(context, appWidgetManager, appWidgetIds); //THIS LINE
DOES NOT GET EXECUTED
if (intent.getAction().equals(ACTION_WIDGET_UPDATE)) {
//
} else if (intent.getAction().equals(SHOW_THREADS)) {
remoteViews.setImageViewResource(R.id.widget_image,
R.drawable.icon_pressed);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
Intent action = new Intent(context, ShowThreads.class);
action.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
action.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
context.startActivity(action);
} else if
(intent.getAction().equals(ShowThreads.ACTION_LOADED)) {
remoteViews.setImageViewResource(R.id.widget_image,
R.drawable.icon);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
if
(intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_DELETED))
{
final int appWidgetId = intent.getExtras().getInt
(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
this.onDeleted(context, new int[] { appWidgetId });
}
} else if
(intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {
//do nothing
} else if
(intent.getAction().equals("android.intent.action.PACKAGE_CHANGED")) {
//do nothing
} else if
(intent.getAction().equals("android.intent.action.PACKAGE_INSTALL")) {
//do nothing
} else if
(intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {
//do nothing
} else if
(intent.getAction().equals("android.intent.action.PACKAGE_REPLACED"))
{
//do nothing
} else if
(intent.getAction().equals("android.intent.action.PACKAGE_RESTARTED"))
{
//do nothing
}
//
super.onReceive(context, intent);
}
//
}
The Receiver intent filter:
<receiver android:name=".MyAppWidget" android:label="@string/
app_name">
<intent-filter>
<action
android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.me.SHOW_THREADS" />
<action android:name="com.me.WIDGET_UPDATE" />
<action android:name="com.me.LOADED" />
<action
android:name="android.intent.action.PACKAGE_ADDED" />
<action
android:name="android.intent.action.PACKAGE_CHANGED" />
<action
android:name="android.intent.action.PACKAGE_INSTALL" />
<action
android:name="android.intent.action.PACKAGE_REMOVED" />
<action
android:name="android.intent.action.PACKAGE_REPLACED" />
<action
android:name="android.intent.action.PACKAGE_RESTARTED" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_provider" />
</receiver>
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en