1

I'm trying to convert a Java example that I found for Android to 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
        );
    }
}

The problem is that in the C# world WindowManager doesn't seem to be available in a Service, whereas in Java it is (at least according to the example that I saw.

In C# IWindowManager is available but that doesn't contain the static members.

Is there a way of doing this?

1 Answer 1

3

It appears that instead of using the WindowManager, I can use the class

WindowManagerLayoutParams

So the code:

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

becomes

var layoutParams = new WindowManagerLayoutParams(1, 1, 
    WindowManagerTypes.SystemOverlay, 
    WindowManagerFlags.WatchOutsideTouch, 
    Format.Translucent);
1
  • It seems, that I try something very similar. Did you get it to work? Could you show the complete converted sample? My problem is that I run into a NotSupportedException when trying to Add the view.
    – The Chris
    Mar 14, 2019 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.