TAGS :Viewed: 5 - Published at: a few seconds ago

[ Cannot update Android Widget using AlarmManager and Calendar ]

I want to update my widget but it simply doesn't work! I made it so that the update happens at particular time - a few minutes after I run the app, but widget doesn't get updated. I know this because I am setting the label with the System.currentTimeMillis. When app gets updated this label should have a new values. I did everything as the Android AlarmManager document told me.

views.setTextViewText(R.id.todayDate,System.currentTimeMillis()+"");

So here is what I have done, in both onReceive and onUpdate of

public class CalendarWidget extends AppWidgetProvider 

More Code

Calendar todayMidnight = Calendar.getInstance();
todayMidnight.setTimeInMillis(System.currentTimeMillis());
todayMidnight.add(Calendar.DATE, 0);
todayMidnight.set(Calendar.HOUR_OF_DAY, 23);
todayMidnight.set(Calendar.MINUTE, 2);
todayMidnight.set(Calendar.SECOND, 0);

AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent (context, CalendarWidget.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);

alarm.setRepeating(AlarmManager.RTC, todayMidnight.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);

...

 views.setTextViewText(R.id.todayDate,System.currentTimeMillis()+"");

In Manifest

<receiver
            android:name="com.standardSPenPlannerInhoHwang.CalendarWidget"
            android:theme="@android:style/Theme.Translucent" 
            android:process=":remote"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
                <action android:name="android.intent.action.TIMEZONE_CHANGED" />
                <action android:name="android.intent.action.TIME_SET" />
                <action android:name="android.intent.action.DATE_CHANGED" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@layout/calendar_widget2" />
        </receiver>

But app doesn't get updated at 11:02PM!! This should work, right? (Just note from me updating the widget from my activities work fine. It is just alarm manager that doesn't update the widget!)

Answer 1


is CalendarWidget your receiver class?
you need a receiver class
public class AlarmReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { //Alarm received,update your widget } }

and in your activity:
Intent alarmIntent=new Intent(yourActivity.this,AlarmReceiver.class); PendingIntent pi=PendingIntent.getActivity(context,ID,intent,0); AlarmManager manager =(AlarmManager)getSystemService(Context.ALARM_SERVICE); manager.set(AlarmManager.RTC, ... ,pendingIntent);