안 드 로 이 드 개발 노트 의 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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.