1. Floating window permission application :
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(getApplicationContext())) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
startActivityForResult(intent, 0);
} else {
mIntent = new Intent(MainActivity.this/*需要启动service的activity*/, FloatWindowService.class/*需要启动的service*/);
bindService(mIntent, serviceConnection, Context.BIND_AUTO_CREATE);//直接启动服务方式启动
}
}
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
Call back the result of requesting permission in Activity:
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.canDrawOverlays(this)) {
Intent mIntent = new Intent(MainActivity.this,FloatWindowService.class);
startService(mIntent);//直接启动服务方式启动
}
}
}
}
2. Create a floating window:
wp = new WindowManager.LayoutParams();
mWindowManager = (WindowManager) getApplication().getSystemService(WINDOW_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
wp.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
wp.type = WindowManager.LayoutParams.TYPE_PHONE;
}
wp.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;//悬浮窗外部可点击
wp.gravity = Gravity.LEFT | Gravity.TOP;//悬浮窗弹出显示位置
wp.x = 0;
wp.y = 0;
wp.width = WindowManager.LayoutParams.WRAP_CONTENT;
wp.height = WindowManager.LayoutParams.WRAP_CONTENT;
LayoutInflater inflater = LayoutInflater.from(getApplication());
mFloatLayout = (LinearLayout) inflater.inflate(R.layout.float_window, null, false);
View view = mFloatLayout.findViewById(R.id.playMonitor);//自定义悬浮窗布局
view.setOnTouchListener(this);//设置移动监听,此处view是自定义充满布局的控件
mWindowManager.addView(mFloatLayout, wp);
3. Set up sliding monitoring:
private int mTouchStartX, mTouchStartY, mStartX, mStartY, mTouchCurrentX, mTouchCurrentY;
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
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();
wp.x += mTouchCurrentX - mTouchStartX;
wp.y += mTouchCurrentY - mTouchStartY;
mWindowManager.updateViewLayout(mFloatLayout, wp);
mTouchStartX = mTouchCurrentX;
mTouchStartY = mTouchCurrentY;
}
return true;
}