简体   繁体   中英

How to make transparent activity window always on top in android?

I have created one transparent activity A. In this, I'm using surface view to play the videos. I'm able to touch the apps and activities behind this activity A. But when I click another app, the activity A goes to background and closed in an improper way. I want to make activity A always stay on top of the screen if u click another apps. I tried with many flags but no one is working correct. If want to know how to make activity window always stay on top ?

Please share your suggestions and ideas. Any help would be highly appreciated.

You can use WindowManager.addView() to add your customized View to the Window, and your can set WindowManager.LayoutParams attributes for your View.

Here is a sample:

private WindowManager wm=null;
private WindowManager.LayoutParams wmParams=null;

private MyCustomView myView=null;

private void createView(){
    myView = new MyCustomView(getApplicationContext());
    wm = (WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
    wmParams = new WindowManager.LayoutParams();

    /**
     * Window type: phone.  These are non-application windows providing
     * user interaction with the phone (in particular incoming calls).
     * These windows are normally placed above all applications, but behind
     * the status bar.
     */
    wmParams.type=LayoutParams.TYPE_PHONE;
    wmParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE;
    wmParams.gravity=Gravity.LEFT|Gravity.TOP; 
    wmParams.x=0;
    wmParams.y=0;
    wmParams.width=40;
    wmParams.height=40;
    wm.addView(myView, wmParams);
}

Don't forget add permission in AndroidManifest.xml:

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

Then, you can do anything you want in MyCustomView out of all applications.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM