java 让应用程序在其他应用程序之上运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10266959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:18:31  来源:igfitidea点击:

Having application running above other app

javaandroidandroid-activitydialogmultitasking

提问by Android Joker

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 background you see the launcher:

通常,即使活动设置为对话框,当您切换到我的应用程序时,您会看到我的应用程序,并在后台看到启动器:

BUT, I want the app will go above any app like this: (made in photoshop):

但是,我希望该应用程序能够超越任何这样的应用程序:(在 photoshop 中制作):

I did see this question Creating a system overlay window (always on top), but in ICS there is no functionallity to the layout. Furthermore, I want to give a dialog box from my app without minimizing the other app...

我确实看到了这个问题Creating a system overlay window (always on top),但在 ICS 中,布局没有功能。此外,我想从我的应用程序中提供一个对话框而不最小化其他应用程序...

回答by android developer

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

有很多应用程序可以在所有内容之上显示浮动视图,例如: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:

无论如何,为了实现这个功能,你必须有一个名为“android.permission.SYSTEM_ALERT_WINDOW”的特殊权限,并使用类似的东西:

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.

回答by Danny

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_WINDOWto display an activity on top of another one. You can just display a regular "shrinked" Activity with a transparent background.

我是 Tooleap SDK 的开发者之一,我们也处理过这个问题。基本上,您不需要使用SYSTEM_ALERT_WINDOW来在另一个活动之上显示活动。您可以只显示具有透明背景的常规“缩小”活动。

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.

这是使用常规活动的“浮动”计算器应用程序的示例。请注意,计算器下方的活动属于另一个应用程序。

Tooleap Calculator Screenshot

Tooleap 计算器截图