Monday, October 18, 2010

Cancel Alarm Service in onDisabled()

It's a follow-up work on last exercise "App Widget using Alarm Manager". As mentioned, the code haven't cancel the Alarm Service when all widgets removed from home screen. It's can be done in onDisabled() method of AppWidgetProvider.

First of all, we have to pass the value of alarmManager and pendingIntent to HelloWidgetProvider(which extends AppWidgetProvider) when created in HelloWidgetConfig(the configure activity).

In configOkButtonOnClickListener.onClick() of HelloWidgetConfig.java, add the code to pass the value of alarmManager and pendingIntent to HelloWidgetProvider()
private Button.OnClickListener configOkButtonOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

final Context context = HelloWidgetConfig.this;

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

//RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.hellowidget_layout);
//appWidgetManager.updateAppWidget(mAppWidgetId, views);
HelloWidgetProvider.updateAppWidget(context, appWidgetManager, mAppWidgetId);

Toast.makeText(context, "HelloWidgetConfig.onClick(): " + String.valueOf(mAppWidgetId) , Toast.LENGTH_LONG).show();

//prepare Alarm Service to trigger Widget
Intent intent = new Intent(HelloWidgetProvider.MY_WIDGET_UPDATE);
pendingIntent = PendingIntent.getBroadcast(HelloWidgetConfig.this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 20*1000, pendingIntent);

HelloWidgetProvider.SaveAlarmManager(alarmManager, pendingIntent);

Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}


In HelloWidgetProvider.java, define static instance of AlarmManager and PendingIntent. Implement a method SaveAlarmManager() to sve the values, it will be called from HelloWidgetConfig.java.

Override onDisabled() method to cancel the alarm service.

static AlarmManager myAlarmManager;
static PendingIntent myPendingIntent;

@Override
public void onDisabled(Context context) {
// TODO Auto-generated method stub
//super.onDisabled(context);

myAlarmManager.cancel(myPendingIntent);

Toast.makeText(context, "onDisabled()", Toast.LENGTH_LONG).show();
}

static void SaveAlarmManager(AlarmManager tAlarmManager, PendingIntent tPendingIntent)
{
myAlarmManager = tAlarmManager;
myPendingIntent = tPendingIntent;
}


next: Update Widget in onReceive() method



3 comments:

C.M.Shrestha said...

Hi,
Thanks for the great example.

I am building a app based on your example. How can i restart the alarm manger once the device goes off and then come on or after reboot.

Please answer based on you example.

Android Er said...

Please refer: Auto start a service with Intent of "android.intent.action.BOOT_COMPLETED" action

PsychoSherry said...

Hi. Thanks for the tutorial.
I'm having some trouble implementing it. onDisabled() isn't called when I delete the widget. I don't know what I'm doing wrong.

Thanks for your help.