I wrote an AppWidget for my application so that you can choose the name of the place you like and it shows you an image from that place.
It has configuration activity (I used the same configuration activity of the application itself) when adding the widget for the first time and its processing settings work fine: I select the place that I like and I see pic;
The problem is that when I restart the device (or it wakes up from sleep mode) when I click on it (widget), I go into configuration activity - I change to the desired pic and nothing happens when I debug, I see that :
appWidgetManager.updateAppWidget(mAppWidgetId, views);
mAppWidgetId =
has id, viewsnot null. so what is going on? my idea is that when the device restarts, the widgetId changes, and I do not process it as I should. BTW, if you try the same thing on the emulator, I have no problems, everything works fine
here is my code:
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.dryrun" android:versionCode="3"
android:versionName="1.1" android:installLocation="auto">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/ic_launcher_test"
android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:debuggable="true">
<activity android:name=".MyActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Preferences.EditPreferences">
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</activity>
<receiver android:name=".Widget.testWidget" android:label="@string/app_widget_">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/test_widget__provider" />
</receiver>
<service android:name=".Widget.testWidget$WidgetService" />
<uses-permission android:name="android.permission.BIND_REMOTEVIEWS"></uses-permission>
</application>
</manifest>
appwidget_provider xml
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dip"
android:minHeight="146dip"
android:updatePeriodMillis="0"
android:initialLayout="@layout/test_widget"
/>
Widget class
public class testWidget extends AppWidgetProvider {
public static String PREFENCES_WIDGET_CONFIGURE = "ActionConfigureWidget";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Intent svcIntent = new Intent(context, WidgetService.class);
widgets = appWidgetIds;
context.startService(svcIntent);
}
@Override
public void onReceive(Context context, Intent intent)
{
final String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action))
{
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
{
super.onReceive(context, intent);
}
}
public static void updateWidget(Context context, RemoteViews remoteViews)
{
String Prefix = context.getString(R.string._prefix);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String ToShow = prefs.getString(context.getString(
R.string.Widget_string),
context.getString(R.string.default_string));
String pkgName = context.getPackageName();
int resID = context.getResources().getIdentifier(Prefix + ToShow, "drawable", pkgName);
WidgetController widgetController = WidgetController.getInstance();
widgetController.setRemoteViewImageViewSource(remoteViews, R.id.WidgetImage, resID);
}
public static class WidgetService extends Service
{
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
RemoteViews remoteView = buildRemoteView(this);
WidgetController.getInstance().pushUpdate(
remoteView,
getApplicationContext(),
testWidget.class);
stopSelf();
}
public RemoteViews buildRemoteView(Context context)
{
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.test_widget);
Intent configIntent = new Intent(context, EditPreferences.class);
configIntent.setAction(testWidget.PREFENCES_WIDGET_CONFIGURE);
configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgets[0]);
PendingIntent runtestPendingIntent = PendingIntent.getActivity(context, 0, configIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.WidgetImage, runtestPendingIntent);
WidgetController controller = WidgetController.getInstance();
controller.updateWidget(context, remoteViews);
return remoteViews;
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
int oldOrientation = this.getResources().getConfiguration().orientation;
if(newConfig.orientation != oldOrientation)
{
RemoteViews remoteView = buildRemoteView(this);
WidgetController.getInstance().pushUpdate(
remoteView,
getApplicationContext(),
testWidget.class);
}
}
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
}
}
Class prefences
public class EditPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
Intent intent = getIntent();
m_extras = intent.getExtras();
}
private Bundle m_extras;
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
{
if(key.equals(getString(R.string.rlvntString)))
{
Context ctx = getApplicationContext();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(ctx);
setResult(RESULT_CANCELED);
if (m_extras != null)
{
Intent resultValue = new Intent();
String stringID = AppWidgetManager.EXTRA_APPWIDGET_ID;
mAppWidgetId = m_extras.getInt(
stringID,
AppWidgetManager.INVALID_APPWIDGET_ID);
RemoteViews views = new RemoteViews(ctx.getPackageName(),R.layout.test_widget);
WidgetController.getInstance().updateWidget(ctx, views);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
resultValue.putExtra(stringID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
}
}
}