[Android Software Development] ChemDroid:
Hello all together,

i've got a little problem updating my widget.

I wanted to add a widget to an existing app (Static IP Toggle).

This app changes between DHCP and static IP configuration when started =>

app started -> DHCP

app started again -> static IP, and vice versa.

I got a request from a user of this app, that it would be nice if there would be a widget included, showing which mode is active (either DHCP or static IP), so I added the widget as follows:

Added lines, concerning the former version without widget, are highlighted in blue.

First I declared the Widget in AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chemdroid.staticiptoggle"
android:installLocation="internalOnly" android:versionName="1.2" android:versionCode="3">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".StaticIPToggle"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

[COLOR="Blue"]<receiver android:name=".Widget" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget" />
</receiver>[/COLOR]

</application>
<uses-sdk android:minSdkVersion="8" />

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

</manifest>

The AppWidgetProvider was declared in /xml/widget.xml:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="72dp"
android:minHeight="72dp"
android:updatePeriodMillis="0"
android:initialLayout="@layout/widget_dhcp">
</appwidget-provider>

I created 2 layouts, one for DHCP, one for static IP. Included was an ImageButton for showing either DHCP or static IP.

Widget.java was created, for setting the specified layout and starting the activity for changing the settings:

package com.chemdroid.staticiptoggle;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.widget.RemoteViews;

public class Widget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
int use_static_ip;
RemoteViews remoteViews;

try {
use_static_ip = Settings.System.getInt(context.getContentResolver(), Settings.System.WIFI_USE_STATIC_IP);
if (use_static_ip == 0) { //DHCP
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_dhcp);
} else { //static IP
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_static);
}
Intent call_activity = new Intent(context, StaticIPToggle.class);
PendingIntent pending_call_activity = PendingIntent.getActivity(context, 0, call_activity, 0);
remoteViews.setOnClickPendingIntent(R.id.widget_icon, pending_call_activity);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
} catch (SettingNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

At last, the activity was changed to update the widget, after IP settings have been changed:

package com.chemdroid.staticiptoggle;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.provider.Settings.SettingNotFoundException;
import android.provider.Settings;
import android.widget.Toast;

public class StaticIPToggle extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

int use_static_ip;
try { --> Code für Änderung der Config

[COLOR="Blue"]AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
ComponentName componentName = new ComponentName(getApplicationContext(), Widget.class);
int[] ids = appWidgetManager.getAppWidgetIds(componentName);
Intent update_widget = new Intent();
update_widget.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
update_widget.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
getApplicationContext().sendBroadcast(update_widget);[/COLOR]
} catch (SettingNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finish();
}
}

But now I have this bug:

If the widget is onClicked, it shows the energy-settings-widget for a short period (~0,5sec-1sec) before the desired widget is shown. A screenshot is attached.

First there's the widget as in the left picture, then changes to the middle picture and either stays there or changes to the right picture after ~0,5-1sec.

Unfortunately I can't see any mistake in the code, do you?

Greetz Oli