Android Open Source - RelaxedClock Holiday Clock App Widget






From Project

Back to project page RelaxedClock.

License

The source code is released under:

MIT License

If you think the Android project RelaxedClock listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package uk.me.timlittle.holidayclock;
// www  .  j av a 2 s . c  o  m
import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class HolidayClockAppWidget extends AppWidgetProvider {
  private static final String TAG = "HolidayClockAppWidget";
  private static final int REQUEST_CODE = 1;
    private Context context = null;
    private static final Intent update = new Intent( HolidayClockService.ACTION_UPDATE );

    
    @Override
    public void onUpdate( Context context, 
        AppWidgetManager appWidgetManager, 
        int[] appWidgetIds )
    {
      Log.d( TAG, "onUpdate" );
        this.context = context;
        this.context.startService( update );
        scheduleTimer();
    }
    
    private void scheduleTimer()
    {
        Calendar date = Calendar.getInstance();
        date.set( Calendar.SECOND, 0 );
        date.set( Calendar.MILLISECOND, 0 );
        date.add( Calendar.MINUTE, 1 );
        AlarmManager am =
                (AlarmManager) context.getSystemService(
                        Context.ALARM_SERVICE );
        PendingIntent pi = PendingIntent.getService( context,
                REQUEST_CODE,
                update,
                PendingIntent.FLAG_NO_CREATE );
        if ( pi == null )
        {
            pi = PendingIntent.getService( context,
                    REQUEST_CODE,
                    update,
                    PendingIntent.FLAG_CANCEL_CURRENT );
            am.setRepeating( AlarmManager.RTC,
                    date.getTimeInMillis(),
                    60 * 1000,
                    pi );
            Log.d( TAG, "Alarm created" );
        }
    }
    

    @Override
    public void onDeleted( Context context,
                           int[] appWidgetIds )
    {
        Log.d( TAG, "onDeleted" );
        AppWidgetManager mgr = AppWidgetManager.getInstance( context );
        int[] remainingIds = mgr.getAppWidgetIds(
                new ComponentName( context, this.getClass() ) );
        if ( remainingIds == null || remainingIds.length <= 0 )
        {
            PendingIntent pi = PendingIntent.getService( context,
                    REQUEST_CODE,
                    update,
                    PendingIntent.FLAG_NO_CREATE );
            if ( pi != null )
            {
                AlarmManager am =
                        (AlarmManager) context.getSystemService(
                                Context.ALARM_SERVICE );
                am.cancel( pi );
                pi.cancel();
                Log.d( TAG, "Alarm cancelled" );
            }
        }
    }    
}




Java Source Code List

uk.me.timlittle.holidayclock.HolidayClockAppWidget.java
uk.me.timlittle.holidayclock.HolidayClockService.java
uk.me.timlittle.holidayclock.MainActivity.java
uk.me.timlittle.holidayclock.TimeToWords.java