做了几个widget,觉得widget的UI更新是一个常用的功能,下面就做了一个笔记:
读者,先看明白下面这篇博文:
Android Widget 桌面数字时钟(DigtalClockWidget)实例
http://blog.csdn.net/ouyang_peng/article/details/8820506
下面是我的一个样例,可以查看一下:
1.
package com.android.alarmclock; //hongyu hexiaoming digital clock aolixing 20140227 start import android.app.Service; import java.util.Timer; import java.util.TimerTask; import android.appwidget.AppWidgetManager; import android.content.ComponentName; import android.widget.RemoteViews; import android.content.Intent; import android.os.IBinder; import com.android.deskclock.R; import android.util.Log; import java.util.Calendar; import android.text.format.Time; public class HyDigitalWidgetUpdateService extends Service { private Timer timer; private TimerTask timerTask = new TimerTask() { @Override public void run() { updateDigitalWidgetUI(); } }; @Override public void onCreate() { super.onCreate(); timer = new Timer(); timer.schedule(timerTask, 0, 500); } @Override public IBinder onBind(Intent intent) { return null; } @Override public void onDestroy() { super.onDestroy(); timer.cancel(); timer = null; } private void updateDigitalWidgetUI() { RemoteViews clock = new RemoteViews(getPackageName(), R.layout.digital_appwidget); Log.i("digital_debug","updateDigitalWidgetUI"); Calendar calendar = Calendar.getInstance(); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); //Time time = new Time(); //time.setToNow(); //int hour = time.hour; //int minute = time.minute; Log.i("digital_debug","updateDigitalWidgetUI---hour:"+hour); Log.i("digital_debug","updateDigitalWidgetUI---minute:"+minute); //if(hour > 12){ if(calendar.get(Calendar.AM_PM) == 0){ Log.i("digital_debug","updateDigitalWidgetUI---am:"); clock.setImageViewResource(R.id.time_am_or_pm, R.drawable.hy_digital_widget_time_am); }else{ Log.i("digital_debug","updateDigitalWidgetUI---pm:"); clock.setImageViewResource(R.id.time_am_or_pm, R.drawable.hy_digital_widget_time_pm); } int hour_1 = hour/10; int hour_2 = hour%10; int minute_1 = minute/10; int minute_2 = minute%10; switch (hour_1) { case 0: clock.setImageViewResource(R.id.time_hour_1, R.drawable.hy_digital_widget_time_0); break; case 1: clock.setImageViewResource(R.id.time_hour_1, R.drawable.hy_digital_widget_time_1); break; case 2: clock.setImageViewResource(R.id.time_hour_1, R.drawable.hy_digital_widget_time_2); break; case 3: clock.setImageViewResource(R.id.time_hour_1, R.drawable.hy_digital_widget_time_3); break; case 4: clock.setImageViewResource(R.id.time_hour_1, R.drawable.hy_digital_widget_time_4); break; case 5: clock.setImageViewResource(R.id.time_hour_1, R.drawable.hy_digital_widget_time_5); break; case 6: clock.setImageViewResource(R.id.time_hour_1, R.drawable.hy_digital_widget_time_6); break; case 7: clock.setImageViewResource(R.id.time_hour_1, R.drawable.hy_digital_widget_time_7); break; case 8: clock.setImageViewResource(R.id.time_hour_1, R.drawable.hy_digital_widget_time_8); break; case 9: clock.setImageViewResource(R.id.time_hour_1, R.drawable.hy_digital_widget_time_9); break; default: break; } switch (hour_2) { case 0: clock.setImageViewResource(R.id.time_hour_2, R.drawable.hy_digital_widget_time_0); break; case 1: clock.setImageViewResource(R.id.time_hour_2, R.drawable.hy_digital_widget_time_1); break; case 2: clock.setImageViewResource(R.id.time_hour_2, R.drawable.hy_digital_widget_time_2); break; case 3: clock.setImageViewResource(R.id.time_hour_2, R.drawable.hy_digital_widget_time_3); break; case 4: clock.setImageViewResource(R.id.time_hour_2, R.drawable.hy_digital_widget_time_4); break; case 5: clock.setImageViewResource(R.id.time_hour_2, R.drawable.hy_digital_widget_time_5); break; case 6: clock.setImageViewResource(R.id.time_hour_2, R.drawable.hy_digital_widget_time_6); break; case 7: clock.setImageViewResource(R.id.time_hour_2, R.drawable.hy_digital_widget_time_7); break; case 8: clock.setImageViewResource(R.id.time_hour_2, R.drawable.hy_digital_widget_time_8); break; case 9: clock.setImageViewResource(R.id.time_hour_2, R.drawable.hy_digital_widget_time_9); break; default: break; } switch (minute_1) { case 0: clock.setImageViewResource(R.id.time_minute_1, R.drawable.hy_digital_widget_time_0); break; case 1: clock.setImageViewResource(R.id.time_minute_1, R.drawable.hy_digital_widget_time_1); break; case 2: clock.setImageViewResource(R.id.time_minute_1, R.drawable.hy_digital_widget_time_2); break; case 3: clock.setImageViewResource(R.id.time_minute_1, R.drawable.hy_digital_widget_time_3); break; case 4: clock.setImageViewResource(R.id.time_minute_1, R.drawable.hy_digital_widget_time_4); break; case 5: clock.setImageViewResource(R.id.time_minute_1, R.drawable.hy_digital_widget_time_5); break; case 6: clock.setImageViewResource(R.id.time_minute_1, R.drawable.hy_digital_widget_time_6); break; case 7: clock.setImageViewResource(R.id.time_minute_1, R.drawable.hy_digital_widget_time_7); break; case 8: clock.setImageViewResource(R.id.time_minute_1, R.drawable.hy_digital_widget_time_8); break; case 9: clock.setImageViewResource(R.id.time_minute_1, R.drawable.hy_digital_widget_time_9); break; default: break; } switch (minute_2) { case 0: clock.setImageViewResource(R.id.time_minute_2, R.drawable.hy_digital_widget_time_0); break; case 1: clock.setImageViewResource(R.id.time_minute_2, R.drawable.hy_digital_widget_time_1); break; case 2: clock.setImageViewResource(R.id.time_minute_2, R.drawable.hy_digital_widget_time_2); break; case 3: clock.setImageViewResource(R.id.time_minute_2, R.drawable.hy_digital_widget_time_3); break; case 4: clock.setImageViewResource(R.id.time_minute_2, R.drawable.hy_digital_widget_time_4); break; case 5: clock.setImageViewResource(R.id.time_minute_2, R.drawable.hy_digital_widget_time_5); break; case 6: clock.setImageViewResource(R.id.time_minute_2, R.drawable.hy_digital_widget_time_6); break; case 7: clock.setImageViewResource(R.id.time_minute_2, R.drawable.hy_digital_widget_time_7); break; case 8: clock.setImageViewResource(R.id.time_minute_2, R.drawable.hy_digital_widget_time_8); break; case 9: clock.setImageViewResource(R.id.time_minute_2, R.drawable.hy_digital_widget_time_9); break; default: break; } AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext()); ComponentName componentName = new ComponentName(getApplicationContext(), DigitalAppWidgetProvider.class); appWidgetManager.updateAppWidget(componentName, clock); } } //hongyu hexiaoming digital clock aolixing 20140227 end
2.
public class DigitalAppWidgetProvider extends AppWidgetProvider { private static final String TAG = "DigitalAppWidgetProvider"; //hongyu hexiaoming digital clock aolixing 20140227 start @Override public void onDisabled(Context context) { // TODO Auto-generated method stub context.stopService(new Intent(context, HyDigitalWidgetUpdateService.class)); } @Override public void onEnabled(Context context) { // TODO Auto-generated method stub super.onEnabled(context); context.startService(new Intent(context, HyDigitalWidgetUpdateService.class)); } //hongyu hexiaoming digital clock aolixing 20140227 end
1.有用的相关资料:
这是一个widget开发的样例,可以看明白,就懂得widget开发的基本过程了:
http://blog.csdn.net/silenceburn/article/details/6093074