How to create a service with addView() using WindowManager in Android

689 Views Asked by At

I had gone through many solution available online but I didn't fine appropriate.

Basically i wanted to utilize following parameters using service

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

I have gone through one of the references are there anybody know how to getWindow() in service on android?

if(mConfiguration.orientation==Configuration.ORIENTATION_LANDSCAPE){
    mLayoutParams.screenOrientation=Configuration.ORIENTATION_PORTRAIT;
}
mLayoutParams.softInputMode=WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN;

mLayoutParams.gravity = Gravity.TOP|Gravity.CENTER; 
int flag=0
            |WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN
            |WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            |WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
            ; 
mLayoutParams.flags=flag;

mWindowManager.updateViewLayout(mRootView, mLayoutParams);

In the above code how to utilize the rootview in service?

Looking forward to hearing from you soon.

1

There are 1 best solutions below

0
kAliert On

The variable mRootView refers to the view which was added to the WindowManager.

Lets say you have added a view myView to the WindowManager like this:

class MyService extends Service
{
    private View myView;

    ...
    myView = new View(context);
    mWindowManager.addView(myView, mLayoutParams);
    ...
}

Now you can update the LayoutParams of myView with the updateViewLayout-method:

mWindowManager.updateViewLayout(myView, mLayoutParams)