I am currently working on an app that can draw views over the statusbar.

Here is my code:

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
    v = new StatusView(this);

    lp = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            **WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL**,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
            PixelFormat.TRANSLUCENT);
    wm.addView(v, lp);
    Log.d(TAG, "Added view");
}

However I get a security exception saying I don't have permission to use this view type. So I tried adding several permissions. Here are the ones i've tried so far:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.INTERNAL_SYSTEM_WINDOW"/>
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.BIND_REMOTEVIEWS"/>
<uses-permission android:name="android.permission.STATUS_BAR"/>

Any suggestions? This may not be possible as the statusbar is not considered part of the window.

EDIT:

The specific error is:

android.view.WindowManager$BadTokenException: unable to add window
android.view.ViewRoot$W@40518c50 - permission denied for this window type
有帮助吗?

解决方案

Nevermind, I solved it! The problem wasn't TYPE , but FLAGS.

其他提示

In order to draw views on top of the status bar, this is the combinations that worked for me after reading so many Stackoverflow posts:

  1. You need to have WindowManager of this LayoutParameters on either your service or your activity:

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT,
                statusBarHeight, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | 
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                PixelFormat.TRANSLUCENT);
    
  2. You just need this permission on your android manifest:

    uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"
    

Hope that helps. In my case I just needed to tint the statusbar with a tranluscent yellow or green to indicate that user is currently in an active call.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top