안 드 로 이 드 개발 노트 의 widget UI 업데이트 문제 의 예

8371 단어 androidwidgetupdat
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 개발 의 기본 과정 을 알 수 있 습 니 다.
안 드 로 이 드 widget 개발 인 스 턴 스: 데스크 톱 메모 프로그램의 상세 한 설명 과 소스 코드 (상)
http://blog.csdn.net/silenceburn/article/details/6093074

좋은 웹페이지 즐겨찾기