如何在Android中创建位于所有应用程序之上的UI或窗口小部件?

Abi*_*Abi 8 android android-windowmanager

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

hat*_*cyl 18

如果您只想显示某些内容,即使是锁屏也可以显示在所有内容之上.

如果您想要点击某些内容,可以将其显示在除锁定屏幕之外的任何内容上.

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

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

//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);
Run Code Online (Sandbox Code Playgroud)