如何在服务中获取WindowManager?

问题描述 投票:0回答:1

我正在尝试将我在Android上找到的Java示例转换为C#:

[Service]
public class SampleService :Service
{
    public override IBinder OnBind(Intent intent)
    {
        return null;
    }

    public override void OnCreate()
    {
        base.OnCreate();

        // create a windowManager object - relatively easy
        var windowManager = GetSystemService(Context.WindowService).JavaCast<IWindowManager>();

        // create layoutParams based on WindowManager LayoutParams class 
        // (i.e. static member of class that doesn't exist in service context?!?!
        var layoutParams = new WindowManager.LayoutParams(1, 1,
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
            PixelFormat.TRANSLUCENT
        );
    }
}

问题是在C#世界中,WindowManager似乎在服务中不可用,而在Java中它是(至少根据我看到的例子)。

在C#中,IWindowManager可用,但不包含静态成员。

有办法做到这一点吗?

xamarin xamarin.android
1个回答
2
投票

似乎不是使用WindowManager,而是使用该类

WindowManagerLayoutParams

所以代码:

    LayoutParams layoutParams = new WindowManager.LayoutParams(1, 1,
        WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
        WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
        PixelFormat.TRANSLUCENT
    );

var layoutParams = new WindowManagerLayoutParams(1, 1, 
    WindowManagerTypes.SystemOverlay, 
    WindowManagerFlags.WatchOutsideTouch, 
    Format.Translucent);
© www.soinside.com 2019 - 2024. All rights reserved.