Mar 5, 2014

Digital clock Appwidget using Alarm manager




First you make Appwidget , if you need reference, can reference or my example.

Then you will layout digital clock on your widget.

Fortunately, you can see 'Digital Clock' widget view.

If you use 'Digital Clock'widget on your widget, can seen be below error image.




Because widget not support 'Digital Clock'. Just support 'Analog Clock'.

You can see reference, support widget classes.


A RemoteViews object (and, consequently, an App Widget) can support the following layout classes:
And the following widget classes:
So, you will make a Digital Clock using text view and alarm manager.

Step 1. make a text view.


widget.xml
  1.  <TextView
  2.                     android:id="@+id/widgetclock"
  3.                     android:layout_width="184dp"
  4.                     android:layout_height="71dp"
  5.                     android:gravity="center"
  6.                     android:textColor="#ffffffff"
  7.                     android:text="12:12"
  8.                     android:textSize="60dp"
  9.                     android:textStyle="bold" />
You will typing 'id'.

This is important.

Step 2. register alarm manager.

widget.java

  1. @Override
  2.     public void onUpdate(Context context, AppWidgetManager appWidgetManager,
  3.             int[] appWidgetIds) {
  4.        
  5.         super.onUpdate(context, appWidgetManager, appWidgetIds);
  6.         setTime(context);
  7.         ...
You will typing function for initialize  in 'onUpdate' method.

  1.     public void setTime(Context context){
  2.         PendingIntent service = null;
  3.         final AlarmManager m = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  4.         final Intent si = new Intent(context, alarmService.class);
  5.         if (service == null)
  6.         {
  7.             service = PendingIntent.getService(context, 0, si, PendingIntent.FLAG_CANCEL_CURRENT);
  8.         }
  9.         Log.d("timeService""start");
  10.         m.setRepeating(AlarmManager.RTCSystem.currentTimeMillis(),1000,service);
  11.     }
This function is register alarm manager and start service.

Important is 'setRepeating'. This method is repeated send service every 1sec in my case.

1000 is millisecond unit.

Step 3. make a service.java

service.java

  1.     @Override
  2.     public int onStartCommand(Intent intent, int flags, int startId)
  3.     {
  4.         update();
  5.         return super.onStartCommand(intent, flags, startId);
  6.     }
If service start when 'onStartCommand' start.

So, need a your function in 'onStartCommand' method.

  1.     private void update()
  2.     {
  3.             String time = getTime();
  4.         AppWidgetManager manager = AppWidgetManager.getInstance(this);
  5.         ComponentName weatherwidget = new ComponentName(this, WeatherWidget.class);
  6.         int[]ids = manager.getAppWidgetIds(weatherwidget);
  7.         final int N = ids.length;
  8.         for (int i = 0; i < N; i++){
  9.             int awID = ids[i];
  10.             RemoteViews views = new RemoteViews(getPackageName(), R.layout.widgetlayoutx2);
  11.             views.setTextViewText(R.id.widgetclock,time)         
  12.             manager.updateAppWidget(awID, views);
  13.         }
  14.     }
This function is update your digital clock on appwidget.

First highlight is getting clock infomation.

Second highlight is setting clock in your widget.

  1.     public String getTime(){
  2.        
  3.         String time=null;
  4.         Date date=new Date(System.currentTimeMillis());
  5.         SimpleDateFormat CurTimeFormat=new SimpleDateFormat("HH:mm");
  6.         time=CurTimeFormat.format(date);
  7.         return time;
  8.     }
This is getting current time using java api.

Step last. register AndroidManifest.xml

  1. <service android:enabled="true" android:name=".alarmService" />
If you don't register Manifest, service doesn't work.


Also, you will use alarm manager for widget update.

Because, android widget update time is minimum 30 min.


No comments:

Post a Comment