【发布时间】:2019-12-07 01:14:15
【问题描述】:
在使用 WindowManager 添加之前,我如何知道是否正在显示视图? 我需要在本机拨号器中放置一个叠加层,但有时本机拨号器放在 mi 自定义视图上方,我通过使用 WindowManager 多次添加元素解决了这个问题,但有时视图显示两次。
谢谢!!
private void callStartIncomingCallScreen(Context context, String incomingNumber) {
startIncomingCallScreen(context, incomingNumber);
Timer timer = new Timer();
for (int i = 0; i < 2; i++) {
timer.schedule(new StartIncomingCallScreenTimerTask(context, incomingNumber), 100 * i);
}
}
class StartIncomingCallScreenTimerTask extends TimerTask {
private Context context;
private String incomingNumber;
StartIncomingCallScreenTimerTask(Context context, String incomingNumber) {
this.context = context;
this.incomingNumber = incomingNumber;
}
public void run() {
Intent intent = new Intent(context, IncomingCallGuiService.class);
context.startService(intent);
}
}
然后在 IncomingCallGuiService 中添加如下视图:
final LayoutParams params = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,
LayoutParams.TYPE_SYSTEM_ALERT,
LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
// add the overlay
wm.addView(view, params);
【问题讨论】:
-
我编辑问题并添加代码@AlexLockwood
-
代码没有正确缩进/格式化......很难阅读。
标签: android overlay android-windowmanager