How to implement a popup overlay that can be displayed over any other app in Android

前端 未结 5 877
花落未央
花落未央 2020-12-12 15:37

How can I implement a popup overlay for an app that can be displayed over any other app.

Facebook implemented a very similar feature called Chatheads in its new Fac

相关标签:
5条回答
  • 2020-12-12 16:09

    i know its late to post the answer, i will post it anyway for other people who are seeking the answer

    AndroidFloatingImage

    0 讨论(0)
  • 2020-12-12 16:17

    This is a minimal, simple, and general example of a floating "chathead"-style overlay.

    It uses the following code to make things float:

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    chatHead = new ImageView(this);
    chatHead.setImageResource(R.drawable.floating);
    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.TYPE_PHONE,
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
        PixelFormat.TRANSLUCENT);
    

    The full source source can be found here (under the Apache license).

    0 讨论(0)
  • Full source code is here: http://www.piwai.info/chatheads-basics
    Note: You will need <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

    public class ChatHeadService extends Service {
    
      private WindowManager windowManager;
      private ImageView chatHead;
    
      @Override public IBinder onBind(Intent intent) {
        // Not used
        return null;
      }
    
      @Override public void onCreate() {
        super.onCreate();
    
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    
        chatHead = new ImageView(this);
        chatHead.setImageResource(R.drawable.android_head);
    
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);
    
        params.gravity = Gravity.TOP | Gravity.LEFT;
        params.x = 0;
        params.y = 100;
    
        windowManager.addView(chatHead, params);
      }
    
      @Override
      public void onDestroy() {
        super.onDestroy();
        if (chatHead != null) windowManager.removeView(chatHead);
      }
    }
    
    0 讨论(0)
  • 2020-12-12 16:24

    There's a library for that if you want Messenger like behavior: Bubbles.

    If you prefer implement it yourself look at Window Manager as others suggest. You can also take a look at the source code of Bubbles to get inspired.

    0 讨论(0)
  • 2020-12-12 16:35

    Every Activity, dialog as well as service is attached with a window. Facebook keeps a service running in background and in the service they fetch window manager object by

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    Create Layout Params needed while adding your view

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.TYPE_PHONE,
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
        PixelFormat.TRANSLUCENT);
    

    now add your view to window manager by following method

    windowManager.addView(yourView, params);
    
    0 讨论(0)
提交回复
热议问题