本文介绍了如何创建位于Android所有应用程序之上的UI或小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在Android中创建将位于所有应用程序之上的UI或小部件吗?有些应用程序具有这样的小部件.一个示例在所有应用程序的顶部都有一个相机图标,单击该图标将捕获屏幕.

Can I create an UI or a widget in Android that will sit on top of all applications? There are applications that have widgets like this. One example has a camera icon on top of all the applications that, when clicked, will capture the screen.

推荐答案

如果只想显示某些内容,则可以将其显示在所有内容之上,甚至包括锁屏.

If you want to just display something, you can display it on top of everything even the lockscreen.

如果您希望单击某些内容,则可以将其显示在除锁屏之外的任何内容之上.

If you want something to be clickable, you can display it on top of anything except the lockscreen.

这里是一个示例,请根据您的需要进行修改:

Here's a sample, modify to your needs:

创建服务并执行以下操作:

Create a service and do the following:

//These three are our main components.
WindowManager wm;
LinearLayout ll;
WindowManager.LayoutParams ll_lp;

//Just a sample layout parameters.
ll_lp = new WindowManager.LayoutParams();
ll_lp.format = PixelFormat.TRANSLUCENT;
ll_lp.height = WindowManager.LayoutParams.FILL_PARENT;
ll_lp.width = WindowManager.LayoutParams.FILL_PARENT;
ll_lp.gravity = Gravity.CLIP_HORIZONTAL | Gravity.TOP;

//This one is necessary.
ll_lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;

//Play around with these two.
ll_lp.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
ll_lp.flags = ll_lp.flags | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

//This is our main layout.
ll = new LinearLayout(this);
ll.setBackgroundColor(android.graphics.Color.argb(0, 0, 0, 0));
ll.setHapticFeedbackEnabled(true);

//And finally we add what we created to the screen.
wm.addView(ll, ll_lp);

这篇关于如何创建位于Android所有应用程序之上的UI或小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 08:20