Search code examples
androidnotificationsstatusbar

Android add view in status bar


This is my code for adding view:

        LayoutInflater layoutInflater = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    TextView tv = (TextView) layoutInflater.inflate(R.layout.textview, null);
    final View view = tv;
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, 25,
            WindowManager.LayoutParams.TYPE_STATUS_BAR,
            WindowManager.LayoutParams.FLAG_SCALED
            , PixelFormat.TRANSLUCENT);


    WindowManager wm = (WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE);
    wm.addView(view, lp);

but it's will give me an error:

android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@41668948 -- permission denied for this window type

so i add permission in android manifast

  <uses-permission android:name="android.permission.STATUS_BAR" />

but still error is there, i need help!!


Solution

  • From the code snippet, it like you're trying to create a new status bar window, which is not possible. (There is one status bar and it is managed by the System UI.)

    Can you describe what you're trying to do? In general you interact with the status bar using the Notification and NotificationManager APIs.