我想创建一个更新其内容的视图,并定期在窗口顶部显示。我找到了一个在窗口顶部(http://blog.daum.net/mailss/18)进行查看的示例。该活动创建一个用户界面,如果我触摸了启动服务按钮,该视图将显示其文本。但是当视图创建时,它不会更新其内容。我试图添加简单的代码来计算视图更新的数量,但它不起作用。如何定期更改视图的内容?我应该在服务中添加循环代码吗?我需要你的建议,谢谢你。
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;
}
}
}