/** * 创建者 :赵鹏 时间:2018/10/16 */ public class FloatRoomViewManager { private static WindowManager.LayoutParams mLayoutParams; private static WindowManager mWindowManager; private static FloatRoomView mFloatView; public static Context mContext; public static boolean isFloatViewVisible; public static MQueueBean mMicBean; public static RoomInfoBean.ContentBean mRoomInfoContent; public static void init(Context context){ mContext = context; mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); } public static void showFloatView(String iconPath){ if(mWindowManager == null){ throw new NullPointerException("--- 还未调用 init() 方法 ---"); } if(mLayoutParams == null){ initLayoutParams(); } mFloatView = new FloatRoomView(mContext, iconPath); mFloatView.setParams(mLayoutParams); mWindowManager.addView(mFloatView, mLayoutParams); isFloatViewVisible = true; } private static void initLayoutParams() { mLayoutParams = new WindowManager.LayoutParams(); if(Build.VERSION.SDK_INT >= 26){ mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY; } else if (Build.VERSION.SDK_INT >= 24) { /*android7.0不能用TYPE_TOAST*/ mLayoutParams.type = WindowManager.LayoutParams.TYPE_PHONE; } else { /*以下代码块使得android6.0之后的用户不必再去手动开启悬浮窗权限*/ String packname = mContext.getPackageName(); PackageManager pm = mContext.getPackageManager(); boolean permission = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.SYSTEM_ALERT_WINDOW", packname)); if (permission) { mLayoutParams.type = WindowManager.LayoutParams.TYPE_PHONE; } else { mLayoutParams.type = WindowManager.LayoutParams.TYPE_TOAST; } } mLayoutParams.format = PixelFormat.RGBA_8888; mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; mLayoutParams.gravity = Gravity.LEFT | Gravity.TOP; mLayoutParams.width = (int) mContext.getResources().getDimension(R.dimen.x104); mLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT; mLayoutParams.x = ScreenUtils.getScreenWidth() - mLayoutParams.width - ((int)mContext.getResources().getDimension(R.dimen.x17)); mLayoutParams.y = ScreenUtils.getScreenHeight() / 2; } public static boolean isCanShowFloatView(){ Boolean result = true; if (Build.VERSION.SDK_INT >= 23) { try { Class clazz = Settings.class; Method canDrawOverlays = clazz.getDeclaredMethod("canDrawOverlays", Context.class); result = (Boolean) canDrawOverlays.invoke(null, mContext); } catch (Exception e) { e.printStackTrace(); } } return result; } public static void closeFloatView(){ if(!isFloatViewVisible || mFloatView == null){ return; } mWindowManager.removeView(mFloatView); isFloatViewVisible = false; } public static void startRoomActivity(boolean needSing){ if(!isFloatViewVisible){ return; } ActivityManager mAm = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); //获得当前运行的task List<ActivityManager.RunningTaskInfo> taskList = mAm.getRunningTasks(100); for (ActivityManager.RunningTaskInfo rti : taskList) { //找到当前应用的task,并启动task的栈顶activity,达到程序切换到前台 if(rti.topActivity.getPackageName().equals(mContext.getPackageName())) { mAm.moveTaskToFront(rti.id,0); break; } } //若没有找到运行的task,用户结束了task或被系统释放,则重新启动mainactivity Intent resultIntent = new Intent(mContext, RoomActivity.class); resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); resultIntent.putExtra(KtvConstant.IS_FORM_FLOAT, true); resultIntent.putExtra(KtvConstant.IS_IMMEDIATE_SING, needSing); mContext.startActivity(resultIntent); closeFloatView(); } public static void sendChangeMusic(){ if(mRoomInfoContent == null || TextUtils.isEmpty(mRoomInfoContent.getRid()) || mMicBean == null || mMicBean.getContent() == null || mMicBean.getContent().getContent() == null || mMicBean.getContent().getContent().getMusic() == null || TextUtils.isEmpty(mMicBean.getContent().getContent().getMusic().getId())){ } ApiRetrofit.getInstance().changeMusic("2", mRoomInfoContent.getRid(), mMicBean.getContent().getContent().getMusic().getId()) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new AbsAPICallback<BaseOKResponse>() { @Override public void onNext(BaseOKResponse bean) { } @Override protected void onError(ApiException ex) { } @Override protected void onResultError(ApiException ex) { } }); } }
悬浮窗管理
最新推荐文章于 2021-05-26 17:34:56 发布
该代码实现了一个名为`FloatRoomViewManager`的悬浮窗管理类,用于在Android设备上创建和管理悬浮窗口。类中包含了初始化、显示、关闭悬浮窗的方法,以及检查悬浮窗显示权限的功能。此外,还包含了一个切换音乐的API调用。悬浮窗的位置、大小和权限处理根据Android不同版本进行了适配。
摘要由CSDN通过智能技术生成