I have tried maybe around 7,8 tutorials and read at least 30,40 answers here but in vain...i cant get any widget working on my phone.
Every widget implementation doesnt update the text on my widgets nor it does register widgets on click.
for example this tutorial: http://blog.doityourselfandroid.com/2011/05/24/developing-android-home-screenwidgets/ you can download the code, it runs fine on my friends phone ( android 2.3) but on my phone i can see the receivers firing in Log Cat but nothing updates?
Here is my current simple code:
1.manifest
<receiver android:name=".TestWidgetProvider">
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/testwidgetprovider"
/>
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
</receiver>
<receiver android:name=".TestReceiver"/>
TestWidgetProvider
@Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){ Log.d("CSV", "Called on onUpdate"); AlarmManager am=(AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Calendar cal=Calendar.getInstance(); cal.add(Calendar.SECOND, 30); Intent intent=new Intent(context, TestReceiver.class); int N=appWidgetIds.length; for (int i=0; i<N; i++) { RemoteViews tt=new RemoteViews(context.getPackageName(), R.layout.testwidget); tt.setTextViewText(R.id.textView1,"UPDATED" +i); appWidgetManager.updateAppWidget(appWidgetIds[i], tt); Log.d("CSV", "Update widget, i = " +i + " widgetId=" + appWidgetIds[i] ); } intent.putExtra("id", appWidgetIds); intent.putExtra("duz", appWidgetIds.length); PendingIntent p1=PendingIntent.getBroadcast(context, 1, intent, 0); am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 10000, p1); super.onUpdate(context, appWidgetManager, appWidgetIds);
}
TestReceiver
public class TestReceiver extends BroadcastReceiver { private static final String tag="TestReceiver"; @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub int[] ID; Bundle extras=intent.getExtras(); ID=extras.getIntArray("id"); int duz=intent.getIntExtra("duz", 0); Log.d("CSV", "update widget id=" +ID); double b=Math.random(); int i=(int) ((b*10+1)%10); RemoteViews rv=new RemoteViews(context.getApplicationContext().getPackageName(), R.layout.testwidget); AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext()); for (int a=0; a<duz; i++) { rv.setTextViewText(R.id.textView1, "update random: " + a); appWidgetManager.updateAppWidget(ID[a], rv); Log.d("CSV", "Update widget, a = " +a + " widgetId=" + ID[a] ); } /* ComponentName provider = new ComponentName(context, TestWidgetProvider.class); appWidgetManager.updateAppWidget ( provider, rv); */
}
widget xml
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="200dp" android:minHeight="120dp" android:updatePeriodMillis="0" android:initialLayout="@layout/testwidget" android:resizeMode="horizontal|vertical" > </appwidget-provider>
Please help i've been stuck for days know...
Edit 1. I've added a main activity as proposed by first answer, but no change.
Also the text doesnt update pragmatically the first time i set the widget on screen, like it cant find the widget, but the second time i put the widget on screen it changes its text according to the code in onEnable?
Edit 2: I have posted an answer that fixed my issue for now, and got the widget updating
Edit 3. I have marked Richard Le Mesurier's answer as the correct one since his demo code goes into great detail of managing widgets via services while doing network operations. It covers my issue as well as many other ones i was about to ask. I recommend that you study this demo if you are going to do some serious work with widgets.