Having application running above other app

前端 未结 2 808
暗喜
暗喜 2020-12-04 16:58

I want to make an activity that can be opened above ANY app.

Normally, even when the activity is set as dialog, when you switch to my app, you see my app, and in the

2条回答
  •  再見小時候
    2020-12-04 17:45

    there are plenty of apps that show a floating view on top of everything like : airbrowser , LilyPad , Stick it , AirTerm , Smart Taskbar , aircalc ...

    anyway , in order to achieve this feature , you must have a special permission called "android.permission.SYSTEM_ALERT_WINDOW" , and use something like that:

    final WindowManager.LayoutParams param=new WindowManager.LayoutParams();
    param.flags=WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
    final View view=findViewById(R.id.my_floating_view);
    final ViewGroup parent=(ViewGroup)view.getParent();
    if(parent!=null)
      parent.removeView(view);
    param.format=PixelFormat.RGBA_8888;
    param.type=WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
    param.gravity=Gravity.TOP|Gravity.LEFT;
    param.width=parent!=null?LayoutParams.WRAP_CONTENT:view.getLayoutParams().width;
    param.height=parent!=null?LayoutParams.WRAP_CONTENT:view.getLayoutParams().height;
    final WindowManager wmgr=(WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
    wmgr.addView(view,param);
    // TODO handle overlapping title bar and/or action bar
    // TODO you must add logic to remove the view
    // TODO you must use a special permission to use this method :android.permission.SYSTEM_ALERT_WINDOW
    // TODO if you wish to let the view stay when leaving the app, make sure you have a foreground service running.
    

提交回复
热议问题