WindowManager is unable to show Window View on screen even though it's visibility property is true

My application shows UI based on events received in various BroacastReceiver . Since I need to generate view directly from BroadcastReceiver I use code like this :

mWindowManager = (WindowManager) context
    .getSystemService(Context.WINDOW_SERVICE);

    WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
        LayoutParams.FILL_PARENT,
        LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.TYPE_TOAST, 
        WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
        PixelFormat.OPAQUE);

    TextView view = new TextView(context);

    view.setText("You got Broadcast !");

    mainView = view; 
    mWindowManager.addView(view, lp); 
Log.d("MyApp"," View shown ")

This code displays TextView with text "You got Broadcast" properly when I receive BroadcastReceiver event when my main application activity is accessed beforehand. Means if my application is in memory and my main activity has been shown.

Now when I restart phone and my BroadcastReceiver receives event everything works fine. Infact I can see the debug statement "MyApp, View shown" in logcat. But strangely, actual view with text "You got Broadcast" does not appear on screen. Now again I launch my main activity and press home button . Now if BroadcastReceiver again receives event, it is now able to show the texview with text "You got Broadcast" properly.

At present I'm not doing anything in main activity apart from showing default generated xml layout. It's dead simple code :

public class MainActivity extends Activity {
public static final String TAG = "MainActivity";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Log.d(TAG, " Main screen");
}

}

Am I missing some screen/graphics initialization which happens when main activity is launched ? Or do I need to pass on any additional parameter to WindowManager ?


Asked by: David124 | Posted: 25-01-2022






Answer 1

I am also facing the same issue.

I think it's possible that the screen that you want to add the textview to isn't on the foreground yet. For testing purposes, maybe you can try sleeping for a couple seconds before calling .addview()

Answered by: Adelaide871 | Posted: 26-02-2022



Answer 2

Add proper permission inside menifest file . e.g. sysytem overlay , sysytem alert window ,boot completed , set priority in receiver tag. thnks

Answered by: Ada990 | Posted: 26-02-2022



Similar questions

android - listactivity is stuttering caused by WindowManager

i have a question regarding some weird logcat outputs. Scenario: I have a listactivity with images and textviews, etc. The images are loaded by Fedor's implementation of LazyList. I changed his cache to a LRUMap from commons. On scrolling i got sometimes a stuttering and logcats shows me a lot of this messages: V/WindowManager( 2482): Dsptch > Window{48267640 <packagename to Listactiv...


android - How to deal with keyevents when added a view with WindowManager?

I added a custom view with WindowManager, everything is right. Just one problem : I cannot find where to catch keyevents such as 'BACK' pressed. I caught some of the events in method 'View.dispatchKeyEvent()' in my custom view, but not including 'BACK' or 'HOME'. Any Advice? Thanks a lot! My code is just like this: // get WindowManager WindowManager windowManager = (WindowManager) getContext() .g...


android - Adjusting screen brightness using WindowManager stops working after inactivity

I'm using this code to change an Activity's brightness by dimming it. WindowManager.LayoutParams layoutParameters = getWindow().getAttributes(); layoutParameters.screenBrightness = 0.0075f; getWindow().setAttributes(layoutParameters); I'm also adjusting the android.provider.Settings.System brightness. My problem is, that after ~8 seconds of inactivity, the screen brightness resort...


What is the better way of creating Windowmanager instance android

I found the following two ways of creating WindowManager instance WindowManager w = activity.getWindowManager(); and WindowManager windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); but in Motorola Milestone if I use the first approach of creating WindowManager then I am not getting accurate width of my device...


android - Windowmanager with Animation

First of all thanks everyone who tries to reply this topic. I have an activity and I wanted to show a sort of menu at the top of the screen and I used windowmanager to handle this. it was about UI issues I encountered why I choise windowmanager to do such a menu. But for now I want this menu to animate but it seems animation takes no effect. Here is my code. If anyone has any idea how to animate windowmanag...


android - Efficient way to request touch focus from WindowManager?

I have created two views on a FrameLayout, and requested focus to second. When I start the activity touch events hangs up for a while, then passes to my GL view. This takes random time I think (generally 5-10 seconds). It there a way to flush event queue? I have a GLSurfaceView and I want draw an overlay SurfaceView above it. SurfaceView must be drawn on top of GLSurfaceView but input events must be delivered to op...


android - How to fix the WindowManager exception in media player?

android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@405cab80 is not valid; is your activity running?


android - Animate view added on WindowManager

I have a view (customView) added to the WindowManager. WindowManager mWm = (WindowManager)activity.getApplicationContext().getSystemService(Context.WINDOW_SERVICE); WindowManager.LayoutParams(WindowManager.LayoutParams.FILL_PARENT, 0, PixelFormat.TRANSPARENT); mWl.dimAmount = 0.0f; mWm.addView(customView, mWl); Inside the custom view, I will call a translate animation when close button is ...


android - How to capture keypress in View added by WindowManager

I would like to add a custom view for my application. For this I use WindowsManager: final WindowManager wm = getWindowManager(); final View view = ((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.game_menu, null); WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); lp.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND | Wind...


Android: change LayoutParams of View added by WindowManager

I have overlay View managed by WindowManager, just like in this question. Briefly, it looks like this: WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); my_view_layout_params = new WindowManager.LayoutParams( WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, WindowManager.LayoutParams.FLAG_WAT...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top