I am having a weird problem with my configurable clock widget I´m not being able to debug :( This is my first Widget an it´s an easy one ;), It´s a background configurable clock widget. When the user selects the widget the configuration activity is launched. In this activity the user may choose the clock´s widget background. When the user finishes the configuration a "standard clock widget" with the user´s background appears on the launcher´s screen and that´s all. I tested it thoroughly and it seems to works right till... The problem I´ve got it´s that from time to time when I turn on and unlock the screen, my widget it´s "gone" and I´ve got a grey box with the dreaded "Problem loading Widget" error on it :( So, I´ve got an error that I can´t get a reproducible scenario for it, maybe a life-cycle widget method error related? When the error appears I see no problem in the logcat so I´m completely lost.
This is the easy code for this widget: (GB 2.3.3). I´ve taken out not important bits.
1.- Widget Provider xml
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minHeight="190dp"
android:minWidth="170dp"
android:configure="......"
android:initialLayout="@drawable/......"
android:updatePeriodMillis="30000" >
</appwidget-provider>
2.- AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="........"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="........"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
<receiver android:name="........." >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/........" />
</receiver>
</application>
</manifest>
3.- Widget Layout I´ve got several widget layouts.The user selects one from the configuration activity All the layouts are almost identical with minor changes (ImageView drawable):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="160dp"
android:layout_height="200dp"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/...." />
<AnalogClock
android:layout_width="wrap_content"
android:layout_height="138dp"
android:layout_gravity="bottom"
android:dial="@drawable/...."
android:hand_hour="@drawable/...."
android:hand_minute="@drawable/...." />
</FrameLayout>
4.- Widget Provider (Java)
public class ClockProvider extends AppWidgetProvider {
public static int anoEscudo;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
RemoteViews remoteView = null;
for (int appWidgetId : appWidgetIds) {
// Actualizamos la variable para luego poder modificar el fondo
switch (anoEscudo) {
case (...):
remoteView = new RemoteViews(context.getPackageName(), R.layout....);
break;
...
}
appWidgetManager.updateAppWidget(appWidgetId, remoteView);
}
}
}
5.- Configuration Activity (Java)
package ...;
public class ConfigurationActivity extends Activity {
private int appWidgetId;
ImageView escudo;
TextView descripcion;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// get the appWidgetId of the appWidget being configured
Intent launchIntent = getIntent();
Bundle extras = launchIntent.getExtras();
appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
// set the result for cancel first
// if the user cancels, then the appWidget
// should not appear
Intent cancelResultValue = new Intent();
cancelResultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
setResult(RESULT_CANCELED, cancelResultValue);
// show the user interface of configuration
setContentView(R.layout.activity_configuration);
// Setting configuration Activity graphical elements
// ..
}
//Changing the Widget configuration
private void ... (View view) {
switch (escudoSeleccionado) {
case 1:
ClockProvider.anoEscudo = ...;
break;
...
}
}
// Finish the configuration Activity
public void ... (View view) {
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
setResult(RESULT_OK, resultValue);
new ClockProvider().onUpdate(this, AppWidgetManager.getInstance(this), new int[] { appWidgetId });
finish();
}
}