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
<TextView
android:id="@+id/widgetclock"
android:layout_width="184dp"
android:layout_height="71dp"
android:gravity="center"
android:textColor="#ffffffff"
android:text="12:12"
android:textSize="60dp"
android:textStyle="bold" />
You will typing 'id'.
This is important.
Step 2. register alarm manager.
widget.java
@Override
public void onUpdate
(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
setTime(context);
...
You will typing function for initialize in 'onUpdate' method.
public void setTime
(Context context
){
PendingIntent service = null;
final AlarmManager m =
(AlarmManager
) context.
getSystemService(Context.
ALARM_SERVICE);
final Intent si = new Intent(context, alarmService.class);
if (service == null)
{
service = PendingIntent.getService(context, 0, si, PendingIntent.FLAG_CANCEL_CURRENT);
}
Log.d("timeService", "start");
m.setRepeating(AlarmManager.RTC, System.currentTimeMillis(),1000,service);
}
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
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
update();
return super.onStartCommand(intent, flags, startId);
}
If service start when 'onStartCommand' start.
So, need a your function in 'onStartCommand' method.
private void update()
{
-
AppWidgetManager manager = AppWidgetManager.getInstance(this);
ComponentName weatherwidget = new ComponentName(this, WeatherWidget.class);
int[]ids = manager.getAppWidgetIds(weatherwidget);
final int N = ids.length;
for (int i = 0; i < N; i++){
int awID = ids[i];
RemoteViews views = new RemoteViews(getPackageName(), R.layout.widgetlayoutx2);
views.setTextViewText(R.id.widgetclock,time);
manager.updateAppWidget(awID, views);
}
}
This function is update your digital clock on appwidget.
First highlight is getting clock infomation.
Second highlight is setting clock in your widget.
-
-
-
-
time=CurTimeFormat.format(date);
return time;
}
This is getting current time using java api.
Step last. register AndroidManifest.xml
<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.