android在悬浮窗循环显示时间,过一段时间后卡死?
开始的时候开可以,几分钟就出现卡死,不能拖动的现象?
private FloatViewUtils(Context mContext) {
this.mContext = mContext;
manager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
floatView = new FloatView(mContext);
}
public static FloatViewUtils getInstance(Context mContext) {
if (null == instance) {
synchronized (FloatViewUtils.class) {
if (null == instance) {
instance = new FloatViewUtils(mContext);
}
}
}
return instance;
}
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
public void addFloatView() {
lp.width = floatView.WIDTH;
lp.height = floatView.HEIGHT;
lp.gravity = Gravity.LEFT | Gravity.CENTER;
lp.type = WindowManager.LayoutParams.TYPE_PHONE;
lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
lp.format = PixelFormat.RGBA_8888;
manager.addView(floatView, lp);
new TimeThread().start();
floatView.setOnTouchListener(new View.OnTouchListener() {
//...
});
}
public class TimeThread extends Thread {
@Override
public void run () {
do {
try {
Thread.sleep(1000);
Message msg = new Message();
msg.what = msgKey1;
mHandler.sendMessage(msg);
}
catch (InterruptedException e) {
e.printStackTrace();
}
floatView.postInvalidate();
} while(true);
}
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage (Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case msgKey1:
long sysTime = System.currentTimeMillis();
CharSequence sysTimeStr = DateFormat.format("HH:mm:ss", sysTime);
strText = sysTimeStr.toString();
floatView.text = strText.toString();
break;
default:
break;
}
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
https://segmentfault.com/p/12...