安卓悬浮窗设置

1、悬浮窗不能在当前app中展示
使用activity中的moveTaskToBack(true),获得的悬浮窗效果。

要修改AndroidManifest.xml中
对应的VideoChatViewActivity的模式一定不要用singleTop,修改成singleInstance
如果用singleTop,会认为当前activity就是root,不能获得想要的结果。
一定要修改成这样

2、悬浮窗开发
a、语音通话
浮窗显示一张图片

b、视频通话
浮窗需要显示对方的视频

上代码
1、service

package ;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;


import io.agora.rtc.video.VideoCanvas;

/**
 * 缩小悬浮框
 *
 */
public class FloatVideoWindowService extends Service {
    private WindowManager mWindowManager;
    private WindowManager.LayoutParams wmParams;
    //private LayoutInflater inflater;

    //constant
    private boolean clickflag;

    //view
    private View mFloatingLayout;    //浮动布局
    private FrameLayout mLocalContainer;
    private LinearLayout smallSizePreviewLayout; //容器父布局

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    public class MyBinder extends Binder {
        public FloatVideoWindowService getService() {
            return FloatVideoWindowService.this;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initWindow();//设置悬浮窗基本参数(位置、宽高等)
        initFloating();//悬浮框点击事件的处理
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        removeView();
    }

    public void removeView(){
        if (mFloatingLayout != null) {
            // 移除悬浮窗口
            mWindowManager.removeView(mFloatingLayout);
        }
    }

    /**
     * 设置悬浮框基本参数(位置、宽高等)
     */
    private void initWindow() {
        mWindowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        wmParams = getParams();//设置好悬浮窗的参数
        // 悬浮窗默认显示以左上角为起始坐标
        wmParams.gravity = Gravity.LEFT | Gravity.TOP;
        //悬浮窗的开始位置,因为设置的是从左上角开始,所以屏幕左上角是x=0;y=0
        wmParams.x = 70;
        wmParams.y = 210;
        //得到容器,通过这个inflater来获得悬浮窗控件
        //inflater = LayoutInflater.from(getApplicationContext());
        // 获取浮动窗口视图所在布局
        mFloatingLayout = LayoutInflater.from(getApplicationContext()).inflate(R.layout.alert_float_video_layout, null);
        // 添加悬浮窗的视图
        mWindowManager.addView(mFloatingLayout, wmParams);
    }


    private WindowManager.LayoutParams getParams() {
        wmParams = new WindowManager.LayoutParams();
        //设置window type 下面变量2002是在屏幕区域显示,2003则可以显示在状态栏之上
        //wmParams.type = WindowManager.LayoutParams.TYPE_TOAST;
        //wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        if (Build.VERSION.SDK_INT>=26) {//8.0新特性
            wmParams.type= WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        }else{
            wmParams.type= WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        }
        //设置可以显示在状态栏上
        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR |
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;

        //设置悬浮窗口长宽数据
        wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        return wmParams;
    }

    private void initFloating() {
        smallSizePreviewLayout = mFloatingLayout.findViewById(R.id.small_size_preview);

        //悬浮框点击事件
        smallSizePreviewLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //在这里实现点击重新回到Activity
                Intent intent = new Intent(getApplicationContext(), VideoChatViewActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
                startActivity(intent);
            }
        });

        //悬浮框触摸事件,设置悬浮框可拖动
        smallSizePreviewLayout.setOnTouchListener(new FloatingListener());
    }

    //开始触控的坐标,移动时的坐标(相对于屏幕左上角的坐标)
    private int mTouchStartX, mTouchStartY, mTouchCurrentX, mTouchCurrentY;
    //开始时的坐标和结束时的坐标(相对于自身控件的坐标)
    private int mStartX, mStartY, mStopX, mStopY; //判断悬浮窗口是否移动,这里做个标记,防止移动后松手触发了点击事件
    private boolean isMove;

    private class FloatingListener implements View.OnTouchListener {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    isMove = false;
                    mTouchStartX = (int) event.getRawX();
                    mTouchStartY = (int) event.getRawY();
                    mStartX = (int) event.getX();
                    mStartY = (int) event.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    mTouchCurrentX = (int) event.getRawX();
                    mTouchCurrentY = (int) event.getRawY();
                    wmParams.x += mTouchCurrentX - mTouchStartX;
                    wmParams.y += mTouchCurrentY - mTouchStartY;
                    mWindowManager.updateViewLayout(mFloatingLayout, wmParams);

                    mTouchStartX = mTouchCurrentX;
                    mTouchStartY = mTouchCurrentY;
                    break;
                case MotionEvent.ACTION_UP:
                    mStopX = (int) event.getX();
                    mStopY = (int) event.getY();
                    if (Math.abs(mStartX - mStopX) >= 1 || Math.abs(mStartY - mStopY) >= 1) {
                        isMove = true;
                    }
                    break;
            }

            //如果是移动事件不触发OnClick事件,防止移动的时候一放手形成点击事件
            return isMove;
        }
    }

    /**
     * 添加surfaceview到smallSizePreviewLayout
     */
    /*public void addIntoSmallSizePreviewLayout(View view) {
        if (view.getParent() != null) {
            ((ViewGroup) view.getParent()).removeView(view);
        }

        smallSizePreviewLayout.addView(view);
    }*/

    public void addIntoSmallSizePreviewLayout(VideoCanvas canvas) {
        if (canvas != null) {
            ViewParent parent = canvas.view.getParent();
            if (parent != null) {
                ViewGroup group = (ViewGroup) parent;
                group.removeView(canvas.view);
            }
            if (canvas.view instanceof SurfaceView) {
                ((SurfaceView) canvas.view).setZOrderMediaOverlay(true);
            }
            smallSizePreviewLayout.addView(canvas.view);
        }
    }
}

2、activity

private void startVideoService() {
    LogUtil.i("startVideoService begin");
    //最小化Activity
    moveTaskToBack(true);
    //Constents.mVideoViewLayout = mVideoViewLayout;
    //开启服务显示悬浮框
    Intent floatVideoIntent = new Intent(this, FloatVideoWindowService.class);
    floatVideoIntent.putExtra("userId", getUserId());
    mServiceBound = bindService(floatVideoIntent, mVideoCallServiceConnection, Context.BIND_AUTO_CREATE);
    LogUtil.i("startVideoService end");
}

/**
 * 定义服务绑定的回调 开启视频通话服务连接onLocalAudioMuteClicked
 */
private ServiceConnection mVideoCallServiceConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName name, IBinder iBinder) {
        // 获取服务的操作对象
        floatVideoWindowService = ((FloatVideoWindowService.MyBinder) iBinder).getService();
        LogUtil.i("floatVideoWindowService init");
        if (floatVideoWindowService != null){
            LogUtil.i("floatVideoWindowService init1");
            floatVideoWindowService.addIntoSmallSizePreviewLayout(mRemoteVideo);
            LogUtil.i("floatVideoWindowService init2");
        }
        LogUtil.i("floatVideoWindowService init end");
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        floatVideoWindowService = null;
    }
};

@Override
protected void onRestart() {
    super.onRestart();
    LogUtil.i("floatVideoWindowService onRestart start");
    //从悬浮窗进来后重新设置画布(判断是不是接通了)
    if (mServiceBound) {
        /*//延迟重新加载远端和本地的视频画布
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {


            }
        }, 800);*/
        if (mServiceBound && floatVideoWindowService != null){
            floatVideoWindowService = null;
            unbindService(mVideoCallServiceConnection);
            mServiceBound = false;
        }
        reInitView();

    }
    LogUtil.i("floatVideoWindowService onRestart end");
}

3、布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right">

    <FrameLayout
        android:layout_width="80dp"
        android:layout_height="110dp"
        android:background="@color/black_1f2d3d">

        <LinearLayout
            android:id="@+id/small_size_preview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/transparent"
            android:orientation="vertical" />
    </FrameLayout>
</LinearLayout>

4、授权,老版本不需要,新版本则需要授权

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
       requestAlertWindowPermission();
   }
}

@RequiresApi(api = Build.VERSION_CODES.M)
private void requestAlertWindowPermission() {
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
       Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
               Uri.parse("package:" + getPackageName()));
       startActivityForResult(intent, 0);
   }
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值