Creating a system overlay window (always on top)

前端 未结 16 936
南方客
南方客 2020-11-21 07:07

I\'m trying to create an always-op-top button/clickable-image which stays on top of all the windows all the time.

The proof of concept is

  • here - Smar
相关标签:
16条回答
  • 2020-11-21 07:13

    This is an old Question but recently Android has a support for Bubbles. Bubbles are soon going to be launched but currently developers can start using them.They are designed to be an alternative to using SYSTEM_ALERT_WINDOW. Apps like (Facebook Messenger and MusiXMatch use the same concept).

    Bubbles are created via the Notification API, you send your notification as normal. If you want it to bubble you need to attach some extra data to it. For more information about Bubbles you can go to the official Android Developer Guide on Bubbles.

    0 讨论(0)
  • 2020-11-21 07:14

    WORKING ALWAYS ON TOP IMAGE BUTTON

    first of all sorry for my english

    i edit your codes and make working image button that listens his touch event do not give touch control to his background elements.

    also it gives touch listeners to out of other elements

    button alingments are bottom and left

    you can chage alingments but you need to chages cordinats in touch event in the if element

    import android.annotation.SuppressLint;
    
    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.PixelFormat;
    import android.os.IBinder;
    import android.util.Log;
    import android.view.Gravity;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;
    import android.view.ViewGroup;
    import android.view.WindowManager;
    import android.widget.Toast;
    
    public class HepUstte extends Service {
        HUDView mView;
    
        @Override
        public void onCreate() {
            super.onCreate();   
    
            Toast.makeText(getBaseContext(),"onCreate", Toast.LENGTH_LONG).show();
    
            final Bitmap kangoo = BitmapFactory.decodeResource(getResources(),
                    R.drawable.logo_l);
    
    
            WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    kangoo.getWidth(), 
                    kangoo.getHeight(),
                    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    |WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                     PixelFormat.TRANSLUCENT);
    
    
    
    
    
    
            params.gravity = Gravity.LEFT | Gravity.BOTTOM;
            params.setTitle("Load Average");
            WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    
    
    
            mView = new HUDView(this,kangoo);
    
            mView.setOnTouchListener(new OnTouchListener() {
    
    
                @Override
                public boolean onTouch(View arg0, MotionEvent arg1) {
                    // TODO Auto-generated method stub
                    //Log.e("kordinatlar", arg1.getX()+":"+arg1.getY()+":"+display.getHeight()+":"+kangoo.getHeight());
                    if(arg1.getX()<kangoo.getWidth() & arg1.getY()>0)
                    {
                     Log.d("tıklandı", "touch me");
                    }
                    return false;
                }
                 });
    
    
            wm.addView(mView, params);
    
    
    
            }
    
    
    
        @Override
        public IBinder onBind(Intent arg0) {
            // TODO Auto-generated method stub
            return null;
        }
    
    }
    
    
    
    @SuppressLint("DrawAllocation")
    class HUDView extends ViewGroup {
    
    
        Bitmap kangoo;
    
        public HUDView(Context context,Bitmap kangoo) {
            super(context);
    
            this.kangoo=kangoo;
    
    
    
        }
    
    
        protected void onDraw(Canvas canvas) {
            //super.onDraw(canvas);
    
    
            // delete below line if you want transparent back color, but to understand the sizes use back color
            canvas.drawColor(Color.BLACK);
    
            canvas.drawBitmap(kangoo,0 , 0, null); 
    
    
            //canvas.drawText("Hello World", 5, 15, mLoadPaint);
    
        }
    
    
        protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
        }
    
        public boolean onTouchEvent(MotionEvent event) {
            //return super.onTouchEvent(event);
           // Toast.makeText(getContext(),"onTouchEvent", Toast.LENGTH_LONG).show();
    
            return true;
        }
    }
    
    0 讨论(0)
  • 2020-11-21 07:14

    TYPE_SYSTEM_OVERLAY This constant was deprecated in since API level 26. Use TYPE_APPLICATION_OVERLAY instead. or **for users below and above android 8

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
    } else {
        LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
    }
    
    0 讨论(0)
  • 2020-11-21 07:14

    If anyone still reading this thread and not able to get this working, I'm very sorry to tell you this way to intercept motion event is considered as bug and fix in android >=4.2.

    The motion event you intercepted, although has action as ACTION_OUTSIDE, return 0 in getX and getY. This means you can not see all the motion position on screen, nor can you do anything. I know the doc said It will get x and y, but the truth is it WILL NOT. It seems that this is to block key logger.

    If anyone do have a workaround, please leave your comment.

    ref: Why does ACTION_OUTSIDE return 0 everytime on KitKat 4.4.2?

    https://code.google.com/p/android/issues/detail?id=72746

    0 讨论(0)
  • 2020-11-21 07:15

    I'm one of the developers of the Tooleap SDK. We also provide a way for developers to display always on top windows and buttons, and and we have dealt with a similar situation.

    One problem the answers here haven't addressed is that of the Android "Secured Buttons".

    Secured buttons have the filterTouchesWhenObscured property which means they can't be interacted with, if placed under a window, even if that window does not receive any touches. Quoting the Android documentation:

    Specifies whether to filter touches when the view's window is obscured by another visible window. When set to true, the view will not receive touches whenever a toast, dialog or other window appears above the view's window. Refer to the {@link android.view.View} security documentation for more details.

    An example of such a button is the install button when you try to install third party apks. Any app can display such a button if adding to the view layout the following line:

    android:filterTouchesWhenObscured="true"
    

    If you display an always-on-top window over a "Secured Button", so all the secured button parts that are covered by an overlay will not handle any touches, even if that overlay is not clickable. So if you are planing to display such a window, you should provide a way for the user to move it or dismiss it. And if a part of your overlay is transparent, take into account that your user might be confused why is a certain button in the underlying app is not working for him suddenly.

    0 讨论(0)
  • 2020-11-21 07:17

    Actually, you can try WindowManager.LayoutParams.TYPE_SYSTEM_ERROR instead of TYPE_SYSTEM_OVERLAY. It may sound like a hack, but it let you display view on top of everything and still get touch events.

    0 讨论(0)
提交回复
热议问题