【问题标题】:check if a view is showing with windowmanager检查视图是否正在使用 windowmanager 显示
【发布时间】: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


【解决方案1】:

你可以用你膨胀的视图来检查它。您可以使用view.isShown() 进行检查。当您使用 windowManager.addView(yourView) 添加视图时,调用 isShown() 时返回 true。

希望对你有帮助。

【讨论】:

  • 我认为是 view.isShown() (至少对于最新的 api)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-12
  • 2015-09-05
  • 1970-01-01
  • 2016-09-12
  • 2014-06-08
  • 2020-08-10
  • 2011-11-17
相关资源
最近更新 更多