아래와 같이 서비스를 생성했습니다.이 서비스는 시간을 표시하는 텍스트보기로 시스템 오버레이를 만듭니다. 나는 서비스 클래스의 public 메소드를 호출하여 현재 시간으로 textView를 업데이트한다고 가정하는 인라인 타이머 클래스를가집니다. 메소드를 실행하고 메소드에 배치 된 로그를 통해 새 시간을 전달한다는 것을 알고 있지만 텍스트보기에서 텍스트를 처음으로 변경하는 것으로 보입니다.표면 오버레이에서 텍스트보기를 업데이트 할 수 없습니다.
public class cusNavOverrideService extends Service {
Timer timer;
WindowManager.LayoutParams handleParams;
WindowManager.LayoutParams clockParams;
View screenBlock;
View disableStatusBar;
WindowManager winMgr;
TextView clock;
@Override
public void onCreate() {
winMgr = (WindowManager)getSystemService(WINDOW_SERVICE);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
disableStatusBar = new TableLayout(getApplicationContext());
handleParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FILL_PARENT,
50,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
PixelFormat.TRANSPARENT);
handleParams.gravity = Gravity.TOP;
handleParams.y = 752;
clockParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FILL_PARENT,
50,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
PixelFormat.TRANSPARENT);
clockParams.y = 752;
disableStatusBar.setBackgroundResource(R.drawable.cust_bar);
winMgr.addView(disableStatusBar, handleParams);
Typeface tf = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/Futura Bold.otf");
clock = new TextView(getApplicationContext());
clock.setTextColor(Color.BLACK);
clock.setTextSize(30.0f);
clock.setWidth(50);
clock.setTypeface(tf);
clock.setGravity(Gravity.TOP);
clock.setGravity(Gravity.RIGHT);
clock.setPadding(0, 5, 50, 0);
winMgr.addView(clock, clockParams);
timer = new Timer();
uiCheckTask tTask = new uiCheckTask();
tTask.setService(this);
timer.schedule(tTask, 100, 200);
return START_REDELIVER_INTENT;
}
public void updateClock(String newTime){
Log.v("updateClock", "(((((((((((((((((((("+newTime+")))))))))))))))))");
clock.setText(newTime);
}
@Override
public IBinder onBind(Intent intent) {
// We don't provide binding, so return null
return null;
}
@Override
public void onDestroy() {
timer.cancel();
winMgr.removeView(disableStatusBar);
//Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
}
class uiCheckTask extends TimerTask {
cusNavOverrideService myService;
String newTime;
String tempTime;
Time cTime;
String displayTime;
public void setService(cusNavOverrideService service){
myService = service;
cTime= new Time(Time.getCurrentTimezone());
}
public void run() {
try {
cTime.setToNow();
displayTime = cTime.format("%I:%M%P");
if(displayTime.startsWith("0")){
//displayTime ="CHUCK";
newTime = displayTime.substring(1);
}else{
newTime = displayTime;
}
myService.updateClock(newTime);
}
catch(Exception ex)
{
Log.v("WINDOW CHANGE", "exemption:"+ex);
}
}
}
}
좋아요, 그래서 UI 스레드를 업데이트 할 수 있습니다 – erik
아 .. 알 겠어! 덕분에 – erik