Having application running above other app

亡梦爱人 提交于 2019-11-28 04:01:35

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.

I'm one of the developers of the Tooleap SDK, and we also dealt with this issue. Basically, you don't need to use the SYSTEM_ALERT_WINDOW to display an activity on top of another one. You can just display a regular "shrinked" Activity with a transparent background.

To make a "shrinked Activity, change the activity window layout params of height and width:

WindowManager.LayoutParams params = getWindow().getAttributes(); 
params.x = ...;
params.y = ...;
params.width = ...;
params.height = ...;

this.getWindow().setAttributes(params);

To make a transparent background add to your activity definition in the manifest file:

android:theme="@android:style/Theme.Translucent"

That way, you can create the illusion of a floating activity:

Note that only the foreground activity will be resumed, while the background one is paused. But for most apps this shouldn't be an issue.

Now all that remains is when to launch the floating activity.

Here is an example of a "floating" calculator app using a regular activity. Note that the activity below the calculator belongs to another app.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!