Files
yusheng-android/moduleUtil/src/main/java/com/qxcm/moduleutil/http/FloatingWindowService.java
梁小江 2d510ffe2a pk房完成,剩余禁止对方麦未完成
拍卖房完成
点歌房完成,音乐播放需要测试
2025-07-04 16:38:21 +08:00

182 lines
6.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.qxcm.moduleutil.http;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.IBinder;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import com.alibaba.android.arouter.launcher.ARouter;
import com.qxcm.moduleutil.R;
import com.qxcm.moduleutil.base.CommonAppContext;
import com.qxcm.moduleutil.event.RoomOutEvent;
import com.qxcm.moduleutil.utils.ARouteConstants;
import com.qxcm.moduleutil.utils.ImageLoader;
import com.qxcm.moduleutil.widget.GifAvatarOvalView;
import org.greenrobot.eventbus.EventBus;
public class FloatingWindowService extends Service {
private WindowManager windowManager;
private View floatingView;
@Override
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
// 加载悬浮窗布局
floatingView = LayoutInflater.from(this).inflate(R.layout.floating_window_layout, null);
// 设置 WindowManager.LayoutParams
int layoutType;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
layoutType = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
layoutType = WindowManager.LayoutParams.TYPE_PHONE;
}
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
layoutType,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
layoutParams.gravity = Gravity.TOP | Gravity.START;
layoutParams.x = 100;
layoutParams.y = 100;
// 添加悬浮窗到窗口
windowManager.addView(floatingView, layoutParams);
// 关闭按钮点击事件
// ImageView closeButton = floatingView.findViewById(R.id.iv_guanbi);
// closeButton.setOnClickListener(v -> stopSelf());
setupFloatingViewBehavior();
}
private static final int NOTIFICATION_ID = 1;
@SuppressLint("ForegroundService")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 创建前台通知(必须)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "floating_window_service";
NotificationChannel channel = new NotificationChannel(
channelId,
"悬浮窗服务",
NotificationManager.IMPORTANCE_LOW
);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
Notification notification = new Notification.Builder(this, channelId)
.setContentTitle("悬浮窗正在运行")
.setSmallIcon(R.mipmap.default_avatar) // 替换为你自己的图标资源
.build();
startForeground(NOTIFICATION_ID, notification);
} else {
Notification notification = new Notification.Builder(this)
.setContentTitle("悬浮窗正在运行")
.setSmallIcon(R.mipmap.default_avatar)
.build();
startForeground(NOTIFICATION_ID, notification);
}
return START_STICKY;
}
private void setupFloatingViewBehavior() {
ImageView ivGuanbi = floatingView.findViewById(R.id.iv_guanbi);
GifAvatarOvalView riv = floatingView.findViewById(R.id.riv);
// 点击关闭按钮
ivGuanbi.setOnClickListener(v -> topAndReleaseResources());
// 动画任务
final Runnable mRivAnimationTask = () -> {
Animation rivRotateAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);
riv.startAnimation(rivRotateAnimation);
};
// 模拟播放状态(可替换为真实数据)
boolean isPlaying = CommonAppContext.getInstance().isPlaying;
if (isPlaying) {
ImageLoader.loadHead(this, riv, CommonAppContext.getInstance().playCover);
riv.postDelayed(mRivAnimationTask, 1000);
floatingView.setVisibility(View.VISIBLE);
} else {
floatingView.setVisibility(View.GONE);
}
// 点击头像跳转房间详情
riv.setOnClickListener(v -> {
String roomId = CommonAppContext.getInstance().playId;
if (!TextUtils.isEmpty(roomId)) {
ARouter.getInstance().build(ARouteConstants.ROOM_DETAILS).withString("form", "首页").withString("roomId", CommonAppContext.getInstance().playId).navigation();
// Intent intent = new Intent(Intent.ACTION_VIEW);
// intent.setPackage(getPackageName());
// intent.setData(Uri.parse("customscheme://roomdetails?form=首页&roomId=" + roomId));
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// startActivity(intent);
}
});
}
private void topAndReleaseResources() {
// 停止动画
// if (riv != null) {
//// riv.clearAnimation();
// }
// 移除回调任务(如果有的话)
if (floatingView != null) {
// riv.removeCallbacks(mRivAnimationTask);
}
// 调用关闭逻辑(可调用 Presenter 或直接调用 SDK
// MvpPre.quitRoom(CommonAppContext.getInstance().playId);
// 设置全局状态为 false
CommonAppContext.getInstance().isPlaying = false;
CommonAppContext.getInstance().isShow = false;
EventBus.getDefault().post(new RoomOutEvent());
// 停止服务自身
stopSelf();
}
@Override
public void onDestroy() {
if (floatingView != null) {
windowManager.removeView(floatingView);
}
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}