2

我创建了一个服务,如下所示。该服务创建了一个系统覆盖层,其中包含一个显示时间的文本视图。我有一个内联计时器类,它假设通过调用 Service 类的公共方法来用当前时间更新 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);
        }

    }

}

}

4

3 回答 3

1

Timer 在它自己的线程中运行,并且只允许 UI 线程更新可见的东西。

于 2012-11-07T15:48:13.057 回答
0

我使用的解决方案是创建一个处理程序而不是 timerTask

于 2012-11-07T16:12:16.767 回答
0

我的视图没有消失和重新出现,我遇到了同样的问题。它必须与您(我们)使用 WindowManager.addView(...) 添加视图这一事实有关。一切正常,只是没有更新。也许无效?

于 2014-07-12T14:56:06.227 回答