1. 使用悬浮框
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
2. 具体实现
package com.pandans.hnairexam; import com.pandans.sunshine.provider.SunShineDB.BookT; import android.app.Application; import android.content.Context; import android.graphics.PixelFormat; import android.util.DisplayMetrics; import android.view.Gravity; import android.view.WindowManager; import android.view.WindowManager.LayoutParams; public class ExamApplication extends Application { private WindowManager.LayoutParams windowParams = null; public WindowManager.LayoutParams getWindowParams() { if (windowParams == null) { createWindowParams(); } return windowParams; } private WindowManager.LayoutParams createWindowParams() { windowParams = new WindowManager.LayoutParams(); windowParams.type = WindowManager.LayoutParams.TYPE_PHONE; windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH; windowParams.format = PixelFormat.TRANSLUCENT; /* * 注意,flag的值可以为: 下面的flags属性的效果形同“锁定”。 悬浮窗不可触摸,不接受任何事件,同时不影响后面的事件响应。 * LayoutParams.FLAG_NOT_TOUCH_MODAL 不影响后面的事件 * LayoutParams.FLAG_NOT_FOCUSABLE 不可聚焦 LayoutParams.FLAG_NOT_TOUCHABLE * 不可触摸 */ // 调整悬浮窗口至左上角,便于调整坐标 windowParams.gravity = Gravity.LEFT | Gravity.TOP; // 以屏幕左上角为原点,设置x、y初始值 windowParams.x = 0; windowParams.y = 0; setDisplay(windowParams); return windowParams; } /** * 拖动更改位置 * * @param x * @param y */ public void updateWindows(int x, int y) { windowParams.y = y; ((WindowManager) getSystemService(WINDOW_SERVICE)).updateViewLayout( mFloatView, windowParams); } public void setDisplay(WindowManager.LayoutParams params) { DisplayMetrics dm = new DisplayMetrics(); ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay() .getMetrics(dm); params.width = LayoutParams.MATCH_PARENT; params.height = (int) (dm.heightPixels * 0.4); // params.height = 200; } private PFloatView mFloatView; public void createView(Context cnt) { if (mFloatView == null) { mFloatView = new PFloatView(cnt); mFloatView.setSpinnerData(cnt, getContentResolver().query( BookT.CONTENT_URI, null, null, null, null)); ((WindowManager) getSystemService(WINDOW_SERVICE)).addView( mFloatView, getWindowParams()); } } public void clearView() { if (mFloatView != null) { ((WindowManager) getSystemService(WINDOW_SERVICE)) .removeView(mFloatView); mFloatView = null; } } }
实际使用过程中发现miui的机器无法弹出悬浮窗,经查证,小米有做个权限,默认悬浮窗都不能弹出。
解决:
1、在桌面找到系统应用“安全中心”
轻击一下即进入了此应用
2、点击右下角的“授权管理”进入
点击第二项“应用权限管理”就进入了详细的管理界面
3、右滑进入权限管理,在最下方找到悬浮窗选修,点击进入
4. 点击图标勾选允许
时间: 2024-11-07 09:43:58