2014-07-01 3 views
0

내용을 업데이트하고 창 상단에 정기적으로 표시하는보기를 만들고 싶습니다. 창 상단에보기를 만드는 예제를 발견했습니다 (http://blog.daum.net/mailss/18). 액티비티는 UI를 만들고 시작 서비스 버튼을 터치하면 뷰에 텍스트가 표시됩니다. 그러나보기가 생성되면 내용이 업데이트되지 않습니다. 보기 업데이트 수를 계산하는 간단한 코드를 추가하려고했지만 작동하지 않습니다. 보기의 내용을 정기적으로 변경하려면 어떻게해야합니까? 서비스에 루프 코드를 추가해야합니까? 네 충고가 필요해. 고마워.항상 상단에있는 Android보기 업데이트

1.AlwaysOnTopActivity.java

public class AlwaysOnTopActivity extends Activity implements OnClickListener { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    findViewById(R.id.start).setOnClickListener(this); //start button 
    findViewById(R.id.end).setOnClickListener(this); //stop button  
} 

@Override 
public void onClick(View v) { 
    int view = v.getId(); 
    if(view == R.id.start) 
     startService(new Intent(this, AlwaysOnTopService.class)); 
    else 
     stopService(new Intent(this, AlwaysOnTopService.class));  
} 

}

2.AlwaysOnTopService.java

public class AlwaysOnTopService extends Service { 
private TextView tv;            
@Override 
public IBinder onBind(Intent arg0) { return null; } 

@Override 
public void onCreate() { 
    super.onCreate(); 

    tv = new TextView(this);  //creating view 
    tv.setText("This view is always on top."); 
    tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18); 
    tv.setTextColor(Color.BLUE); 


    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
     WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, //to always on top 
     WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,//for touch event 
     PixelFormat.TRANSLUCENT);             

    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);  
    wm.addView(tv, params);//We must configure the permission in the manifest 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    if(tv != null)  //terminating the view 
    { 
     ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(tv); 
     tv = null; 
    } 
} 

}

답변

1

here 바와 같이 사용 LocalBroadcastManager. UI를 onReceive 방법으로 업데이트하십시오.