悬浮窗管理

该代码实现了一个名为`FloatRoomViewManager`的悬浮窗管理类,用于在Android设备上创建和管理悬浮窗口。类中包含了初始化、显示、关闭悬浮窗的方法,以及检查悬浮窗显示权限的功能。此外,还包含了一个切换音乐的API调用。悬浮窗的位置、大小和权限处理根据Android不同版本进行了适配。
摘要由CSDN通过智能技术生成
/**
 * 创建者 :赵鹏   时间: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) {
                    }
                });
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值